0

I am having big trouble trying to convert a set of 53 factor variables to numeric. Here are a couple of the functions I tried but none of them are working :

sapply(dataset, function(x) transform(as.character(x)))

and then

sapply(dataset, function(x) transform(as.numeric(x)))

I also tried it with lapply, but same thing...

as.numeric(levels(factor)) 

doesnt work either and finally I tried to do it one by one:

transform(dataset, s1 = as.numeric(s1), s2= as.numeric(s2)...etc)

Could somebody please help me ? I also have a couple of missing values NA and M within the variables so I dont know how I can adjust for that. Thanks !

Jaap
  • 81,064
  • 34
  • 182
  • 193
user3620588
  • 41
  • 1
  • 1
  • 2
  • Can you show us some basic example data? It will make it much easier to fix. Provide a minimal working example (MWE) so we can debug the code. – TARehman May 09 '14 at 13:35
  • 1
    Welcome to SO! In addition to @TARehman, info on how to give a [reproducible example can be found here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Jaap May 09 '14 at 13:41
  • This is one of the most common questions on SO (and elsewhere), answered in the R-FAQ 7.10 . see http://cran.r-project.org/ and click on the FAQ link. – Carl Witthoft May 09 '14 at 14:20

1 Answers1

1

Although you didn't provide a reproducible example, this might work:

df[,c(2:54)] <- as.numeric(as.character(unlist(df[,c(2:54)])))

where c(2:54) stands for the columns you want to change to numeric

Jaap
  • 81,064
  • 34
  • 182
  • 193