I am working with a big dataset that is causing some trouble because some of the columns I the dataset are being treated as factors. How can I convert all of the columns fro
You may be trying to solve the wrong problem, or solve the problem at the wrong place. Often the reason that a column that you think is numeric is read in as a factor is because there are characters where numbers should be in the original data. Converting these to numbers will result in a missing value instead of the intended number (which is better than the wrong number). It may be best to fix the original source of the data so that it is read in correctly.
The next option is to use the colClasses
argument to read.table
and related functions to specify that the columns should be numeric and the conversion will take place automatically. This can even be used (with a couple more steps) to convert "numbers" with "$", "%", or "," in them somewhere.
If these don't work for you and you want to convert the existing data frame then here is one approach:
w <- which( sapply( mydf, class ) == 'factor' )
mydf[w] <- lapply( mydf[w], function(x) as.numeric(as.character(x)) )