[R] How do I coerce numeric factor columns of data frame to vector?
Thomas W Blackwell
tblackw at umich.edu
Tue Sep 9 01:13:00 CEST 2003
Murray -
Suppose your data frame is called mydata. If ALL of the columns were
factors with numeric levels, you could do:
newdata <- as.data.frame(lapply(mydata, function(x) as.numeric(as.character(x))))
(Sorry about the nested functions. I didn't invent these complications.)
When only the columns listed in indx need to be converted, make a
complementary vector indy <- c(19,20,38,39,42:60) (say), then do
newdata <- as.data.frame(lapply(mydata[indx],
function(x) as.numeric(as.character(x))))
alldata <- c(mydata[indy], newdata)[ sort.list(c(indy, indx)) ]
Again, it's just a trick to get the columns back in the original order,
but I think my code above will do it. Best luck. Others will probably
have more elegant solutions.
- tom blackwell - u michigan medical school - ann arbor -
On Tue, 9 Sep 2003, Murray Jorgensen wrote:
> I have just noticed that quite a few columns of a data frame that I am
> working on are numeric factors. For summary() purposes I want them to be
> vectors. I tried, for example
>
> > indx <- c(1:18,21:37,40,41)
> > i <- 0
> > i <- i+1
> > summary(as.vector(sflows1[indx[i]]))
> Length Class Mode
> min.pkt.sz 3000 factor numeric
>
> but this does not give the five-number summary that I want. I know that
> I can go back and modify read.table() to change the class of the
> columns, but I want to change the frame without re-reading it.
>
> Murray
>
> --
> Dr Murray Jorgensen http://www.stats.waikato.ac.nz/Staff/maj.html
> Department of Statistics, University of Waikato, Hamilton, New Zealand
> Email: maj at waikato.ac.nz Fax 7 838 4155
> Phone +64 7 838 4773 wk +64 7 849 6486 home Mobile 021 1395 862
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
More information about the R-help
mailing list