Daniel Mastropietro
2002-Sep-01 19:35 UTC
[R] Converting the columns of a data frame to numeric
Hello, I have a data frame whose columns are factors with numeric levels and I want to convert the columns to numeric so that I can treat the data frame as a matrix. I found a way of doing this but would like to know if there is an easier way. My way is: M <- matrix(ncol=ncol(df) , nrow=nrow(df) , as.numeric(as.matrix(df))) where 'df' is the data frame. I am wondering if there is a function, similar to as.numeric(), that converts each column of the matrix 'as.matrix(df)' to numeric. Thanks Daniel Mastropietro -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Daniel Mastropietro wrote:> > Hello, > > I have a data frame whose columns are factors with numeric levels and I > want to convert the columns to numeric so that I can treat the data frame > as a matrix. > I found a way of doing this but would like to know if there is an easier way. > > My way is: > > M <- matrix(ncol=ncol(df) , nrow=nrow(df) , as.numeric(as.matrix(df))) > > where 'df' is the data frame. > > I am wondering if there is a function, similar to as.numeric(), that > converts each column of the matrix 'as.matrix(df)' to numeric.In your case apply(df, 2, as.numeric) should work (coercing each column to numeric and the data.frame to a matrix). Since it operates on vectors and not on the whole object, this way is less efficient than the way you mentioned above. If you import the data.frame, you might want to tell R that the column should be treated as numeric. Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Martin Maechler
2002-Sep-02 13:36 UTC
[R] Converting the columns of a data frame to numeric
>>>>> "Daniel" == Daniel Mastropietro <mastropi at uwalumni.com> >>>>> on Sun, 01 Sep 2002 16:35:29 -0300 writes:Daniel> Hello, I have a data frame whose columns are factors Daniel> with numeric levels and I want to convert the Daniel> columns to numeric so that I can treat the data Daniel> frame as a matrix. I found a way of doing this but Daniel> would like to know if there is an easier way. Daniel> My way is: Daniel> M <- matrix(ncol=ncol(df), nrow=nrow(df), as.numeric(as.matrix(df))) Daniel> where 'df' is the data frame. Would M <- data.matrix(df) solve your problem? I think it is intended to. Martin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._