[R] vectorized way to combine levels of a factor

Abhijit Dasgupta adasgupt at mail.jci.tju.edu
Wed Apr 9 23:13:55 CEST 2008


For your first problem, you can probably do it in 2 statements:
V3 = ifelse(V2==A,V2,V3)
V3 = ifelse(V2==B|V2==C,D,V3)

If you want to split V1 into (0,a],(a,b],(b,c],(c,1], you can do, quite simply
V1.factor = cut(V1, c(0,a,b,c,1))

Abhijit
On Wed, 9 Apr 2008 13:58:17 -0700
Chang Liu <changisme at hotmail.com> wrote:

> 
> 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.


-- 
Abhijit Dasgupta



More information about the R-help mailing list