[R] Still trying to avoid loops
Rui Barradas
ruipbarradas at sapo.pt
Wed Feb 4 20:49:05 CET 2015
Hello,
Aren't the levels of your example wrong? If the levels are
levels=c('a','b','c'), not c('b', 'a', 'c'), then the following will do
the job.
unname(unlist(tapply(dat$D, dat$S, order)))
Hope this helps,
Rui Barradas
Em 04-02-2015 19:34, Tom Wright escreveu:
> Given a dataframe:
> dat<-data.frame(S=factor(c(rep('a',2),rep('b',1),rep('c',3)),levels=c('b','a','c')),
> D=c(5,1,3,2,3,4))
>
> where S is a subject identifier and D a visit (actually a date in my
> real dataset). I would like to generate another column giving the visit
> number
>
> R=c(2,1,1,1,2,3)
>
> My current solution uses nested loops and is slow and ugly. I've looked
> at by() but can't see how to keep the order of R correct.
>
> Thanks,
> Tom
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
More information about the R-help
mailing list