Re: [R] vectorized way to combine levels of a factor

From: Abhijit Dasgupta <adasgupt_at_mail.jci.tju.edu>
Date: Wed, 09 Apr 2008 17:13:55 -0400

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

______________________________________________
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:16:06 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 - 21: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