Re: [R] factor vector manipulation

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 04 Jun 2005 - 10:18:04 EST

On 6/3/05, Weiwei Shi <helprhelp@gmail.com> wrote:
> Hi,
> I have one question on factor vector.
> I have 3 factor vectors:
>
> a<-factor(c("1", "2", "3"))
> b<-factor(c("a", "b", "c"))
> c<-factor(c("b", "a", "c"))
>
> what I want is like:
> c x
> 1 b 2
> 2 a 1
> 3 c 3
>
> which means, I use b as keys and vector a as values and I find values for c.
>
> I used the following codes:
> x<-c()
> d<-data.frame(b,a)
> for (each in 1:length(c)){ # i don't know why each in c did not work
> tmp<-d$a[d$b==c[each]] # question
> x<-append(x, levels(tmp)[as.integer(tmp)])
> }
>
> data.frame(c,x)
>
> If someone has a better way to do it, please help!!
>
> Also, I don't understand why I have to use levels() since w/o it, i
> only added the index for levels in to x.
>
> BTW, when b is like 60,000, the process is very slow. Should I use hash here?
>
> Thanks,
>
> Weiwei
>
cbind(c, x = a[match(c,b)])



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jun 04 10:23:12 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:22 EST