[R] Still trying to avoid loops
Tom Wright
tom at maladmin.com
Wed Feb 4 21:41:30 CET 2015
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')),
> + 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 3 2
> 6 c 4 3
>
>
>
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> On Wed, Feb 4, 2015 at 3:08 PM, Tom Wright <tom at maladmin.com> wrote:
> 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
> > (650) 467-7374
> >
> > "Data is not information. Information is not knowledge. And
> knowledge
> > is certainly not wisdom."
> > Clifford Stoll
> >
> >
> >
> >
> > On Wed, Feb 4, 2015 at 11:49 AM, Rui Barradas
> <ruipbarradas at sapo.pt> wrote:
> > > 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.
> > >>
> > >
> > > ______________________________________________
> > > 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.
>
> ______________________________________________
> 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