0

I have a DF like data

    Sample1  Sample2  Sample3  
Gene1 1         52       0   
Gene2 20        32       1
Gene3 0         21       5

sapply(DF, class) 

reports character, and I would like to transform those values into numeric. Any help??? Thanks!

Mia Lua
  • 157
  • 1
  • 11
  • 3
    `DF <- lapply(DF, as.numeric)` – LAP Dec 07 '17 at 14:04
  • 5
    @LAP you forgot the `[]` to retain the data.frame class: `DF[] <- lapply(DF, as.numeric)`. – lmo Dec 07 '17 at 14:08
  • Note that LAP's answer gives a list. To make that a dataframe again you subsequently run `DF <- do.call(rbind, DF)` edit: Imo beat me to it with a better answer. (also faster because `do.call(rbind,x)` is slowwwww) – Scientist_jake Dec 07 '17 at 14:11
  • 1
    If you actually have a matrix rather than a data frame then `apply(m, 2, as.numeric)` or `storage.mode(m) <- "numeric"` – G. Grothendieck Dec 07 '17 at 14:16
  • but in that case the colnames and rownames are also transformed to numeric and I want them in character – Mia Lua Dec 07 '17 at 14:18
  • ... @MiaLua That's a confusing statement. Can you post the output of `dput(your_df)` with your_df replaced by your actual data? Because it almost sounds like you're saying that the colnames and rownames are literally in your data and if that's the case then more work needs to be done. – Dason Dec 07 '17 at 14:28
  • `DF[] = as.numeric(unlist(DF))` – dww Dec 07 '17 at 16:08

0 Answers0