Re: [R] Still trying to avoid loops

2015-02-04 Thread Tom Wright
> -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Tom Wright > Sent: Wednesday, February 4, 2015 2:08 PM > To: Rui Barradas > Cc: r-h...@stat.math.ethz.ch > Subject: Re: [R] Still trying to avoid loops > > Thanks, I was not aware of

Re: [R] Still trying to avoid loops

2015-02-04 Thread William Dunlap
> > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Tom Wright > Sent: Wednesday, February 4, 2015 2:08 PM > To: Rui Barradas > Cc: r-h...@stat.math.ethz.ch > Subject: Re: [R] Still trying to avoid loops > > Thanks, I was not aw

Re: [R] Still trying to avoid loops

2015-02-04 Thread David L Carlson
ent of Anthropology Texas A&M University College Station, TX 77840-4352 -Original Message- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Tom Wright Sent: Wednesday, February 4, 2015 2:08 PM To: Rui Barradas Cc: r-h...@stat.math.ethz.ch Subject: Re: [R] Still trying to a

Re: [R] Still trying to avoid loops

2015-02-04 Thread William Dunlap
A useful technique when it is easy to compute a vector from an ordered data.frame but you need to do it for an unordered one is to compute the order vector 'ord', compute the vector from df[ord,], and use df[ord,...] <- vector to reorder the vector. In your case you could do: > dat_2<-data.frame

Re: [R] Still trying to avoid loops

2015-02-04 Thread Tom Wright
Sorry Jim, That messes up on S=='a'. Should be 2,1 not 1,2 Neat answer though and looks like it should be pretty quick after I apply some sorting. On Wed, 2015-02-04 at 15:37 -0500, jim holtman wrote: > > > dat<-data.frame(S=factor(c(rep('a',2),rep('b',1),rep('c',3)),levels=c('b','a','c')), > +

Re: [R] Still trying to avoid loops

2015-02-04 Thread jim holtman
> 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)) > dat S D 1 a 5 2 a 1 3 b 3 4 c 2 5 c 3 6 c 4 > dat$visit <- ave(seq(nrow(dat)), dat$S, FUN = seq_along) > dat S D visit 1 a 5 1 2 a 1 2 3 b 3 1 4 c 2 1 5 c

Re: [R] Still trying to avoid loops

2015-02-04 Thread Tom Wright
No problem with disguise, I'm looking for pretty. On Wed, 2015-02-04 at 12:06 -0800, Bert Gunter wrote: > tapply() (of which by() is essentially a wrapper) **is** a (disguised) > loop (at the R level, of course). > > Cheers, > Bert > > > > Bert Gunter > Genentech Nonclinical Biostatistics > (6

Re: [R] Still trying to avoid loops

2015-02-04 Thread Tom Wright
Thanks, I was not aware of order(). I did deliberately mess up the order of S. The following example breaks your solution dat_2<-data.frame(S=factor(c('a','c','a','b','c','c')), D=c(5,3,1,3,2,4)) which should give the answer c(2,2,1,1,2,3) Your solution does indicate that sortin

Re: [R] Still trying to avoid loops

2015-02-04 Thread Bert Gunter
tapply() (of which by() is essentially a wrapper) **is** a (disguised) loop (at the R level, of course). Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374 "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." Clifford Stoll

Re: [R] Still trying to avoid loops

2015-02-04 Thread Rui Barradas
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<-d

[R] Still trying to avoid loops

2015-02-04 Thread Tom Wright
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