r - Right way to convert data.frame to a numeric matrix, when df also contains strings? -
i have data frame taken .csv-file contains numeric , character values. want convert data frame matrix. containing information numbers (the non-number-rows deleted), should possible convert data frame numeric matrix. however, character matrix.
i found way solve use as.numeric
each , every row, quite time-consuming. quite sure there way kind of if(i in 1:n)
-form, cannot figure out how might work. or way start numeric values, proposed here(making matrix numeric , name orders)?
probably easy thing of :p
the matrix lot bigger, first few rows... here's code:
cbind( as.numeric(sfi.matrix[ ,1]), as.numeric(sfi.matrix[ ,2]), as.numeric(sfi.matrix[ ,3]), as.numeric(sfi.matrix[ ,4]), as.numeric(sfi.matrix[ ,5]), as.numeric(sfi.matrix[ ,6])) # again: social.assistance danger.poverty gini s80s20 low.edu unemployment 0.147 0.125 0.34 5.5 0.149 0.135 0.18683691 0.258 0.229 0.27 3.8 0.211 0.175 0.22329362 0.207 0.119 0.22 3.1 0.139 0.163 0.07170422 0.219 0.166 0.25 3.6 0.114 0.163 0.03638525 0.278 0.218 0.29 4.1 0.270 0.198 0.27407825 0.288 0.204 0.26 3.6 0.303 0.211 0.22372633
thank help!
edit 2: see @flodel's answer. better.
try:
# assuming sfi data.frame as.matrix(sapply(sfi, as.numeric))
edit: or @ carlwitthoft suggested in comments:
matrix(as.numeric(unlist(sfi)),nrow=nrow(sfi))
Comments
Post a Comment