Murray Jorgensen
2003-Sep-08 22:15 UTC
[R] How do I coerce numeric factor columns of data frame to vector?
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
Michael A. Miller
2003-Sep-08 23:06 UTC
[R] How do I coerce numeric factor columns of data frame to vector?
>>>>> "Murray" == Murray Jorgensen <maj at stats.waikato.ac.nz> writes:> 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. Do you want them to be vectors or do you want numeric values? If the later, try as.numeric instead of as.vector:> as.vector(factor(rep(seq(4),3)))[1] "1" "2" "3" "4" "1" "2" "3" "4" "1" "2" "3" "4"> as.numeric(factor(rep(seq(4),3)))[1] 1 2 3 4 1 2 3 4 1 2 3 4> summary(as.vector(factor(rep(seq(4),3))))Length Class Mode 12 character character> summary(as.numeric(factor(rep(seq(4),3))))Min. 1st Qu. Median Mean 3rd Qu. Max. 1.00 1.75 2.50 2.50 3.25 4.00 Mike
Thomas W Blackwell
2003-Sep-08 23:13 UTC
[R] How do I coerce numeric factor columns of data frame to vector?
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 >