-1

I need to convert every value in a data frame from char to numeric. I wrote:

y[,2:ncol(y)]<-sapply(y[,2:ncol(y)], as.numeric)

But since the table is big (20000 columns by 3000 rows) takes a lot of time. Is there a way to do this more efficiently? Thanks

hrbrmstr
  • 71,487
  • 11
  • 119
  • 180
GabyLP
  • 3,218
  • 5
  • 40
  • 58
  • this may help: http://stackoverflow.com/questions/26299159/can-i-programmatically-update-the-type-of-a-set-of-columns-to-factors-in-data – KFB Oct 23 '14 at 00:12
  • 1
    can you avoid the step that turns all values into factors? `dat[] – rawr Oct 23 '14 at 00:19
  • 1
    You can speed it up even more by avoiding this operation on unnecessary columns with an `if` statement. `dat[] – Rich Scriven Oct 23 '14 at 00:25
  • See my answer here http://stackoverflow.com/questions/26391921/how-to-convert-entire-dataframe-to-numeric-while-preserving-decimals/26391969#26391969 – Rich Scriven Oct 23 '14 at 00:30
  • @rawr, ok I avoid converting string to factors.... but still have the issue of converting chars to numbers... and it also takes much time. How can I do it faster? Thanks – GabyLP Oct 23 '14 at 00:52
  • @RichardScriven, thanks, but all the columns from 2 to the last need to be converted to numeric. So my main problem is with sapply performance and not with the selection of the rigth cols. – GabyLP Oct 23 '14 at 00:58
  • Have you tried `lapply`? It's faster – Rich Scriven Oct 23 '14 at 01:01
  • 4
    So you have 20,000 columns and only the first one is not numeric? Sounds like you should really be using a numeric matrix in the first place (see `data.matrix`) and keep that first column in a separate vector, maybe use it as rownames to your matrix if it makes sense. – flodel Oct 23 '14 at 01:54

1 Answers1

0

With dplyr

if all is defined as character already :

y2 <- mutate_all(y, funs(as.numeric))

otherwise convert them to characters first

y2 <- mutate_all(y, function(x) as.numeric(as.character(x)))

(you can overide the dataframe by giving the same name "y" instead of "y2")

R. Prost
  • 1,458
  • 12
  • 21