[R] vectorized way to combine levels of a factor

From: Chang Liu <changisme_at_hotmail.com>
Date: Wed, 09 Apr 2008 13:58:17 -0700

Hi Gurus:  

If I have a large dataset of the form of:  

> x <- data.frame(V1 = runif(10), V2 = sample(c('A','B','C'),10,T)) > x V1 V21 0.2691580 A2 0.8711267 B3 0.2674728 C4 0.3278876 A5 0.1809152 A6 0.2499651 C7 0.9155174 A8 0.8004974 B9 0.7885516 A10 0.9301630 A
And I want a V3 that =V2 if V2=A, and =D if V2=B or C. In other words I want to use a vectorized way to combine some levels, rather than having to loop through a large dataset.  

Similarly, if I want to group V1 into levels, what is a fast way to do it?  

Thank you!
Karen



If you like crossword puzzles, then you'll love Flexicon, a game which comb[[elided Hotmail spam]]

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Apr 2008 - 21:03:50 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Apr 2008 - 22:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive