Re: [R] list problem

From: Berton Gunter <gunter.berton_at_gene.com>
Date: Wed 28 Jul 2004 - 01:57:28 EST

This is also the sort of thing tapply() does well: (note that by() and aggregate() are just fancy wrappers for tapply).

e.g.
tus<-tapply(yourframe$Total,list(yourframe$Year,yourframe$Tus>0),sum)

(One could nest this within an lapply() to loop over the different columns of your frame).

Note that the vectors in the list are silently cast to factors for tapply.

--

Bert Gunter

Non-Clinical Biostatistics
Genentech
MS: 240B
Phone: 650-467-7374


"The business of the statistician is to catalyze the scientific learning
process."

 -- George E.P. Box

Dimitris Rizopoulos wrote:


> Hi Luis,
>
> maybe there are better ways but you could try something like this,
>
> n <- length(frame)
> lapply(split(frame, frame$Year), function(x, n.){
> res <- numeric(n.-2)
> for(i in 3:n.) res[i-2] <- sum(x$Total[x[,i]>0.])
> res
> }, n.=n)
>
> I hope this helps.
>
> Best,
> Dimitris
>
> ----
> Dimitris Rizopoulos
> Doctoral Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
>
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/16/396887
> Fax: +32/16/337015
> Web: http://www.med.kuleuven.ac.be/biostat/
> http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
>
> ----- Original Message -----
> From: "Luis Rideau Cruz" <Luisr@frs.fo>
> To: <r-help@stat.math.ethz.ch>
> Sent: Tuesday, July 27, 2004 2:22 PM
> Subject: [R] list problem
>
> > Hi all,
> >
> > I have the folowing frame(there are more columns than shown),
> > 1 2 3 4 5
> > Year Total Tus Whi Norw
> > 1994 1.00 1830 0 355
> > 1995 1.00 0 0 0
> > 1995 1.00 0 0 0
> > 1995 1.00 4910 4280 695
> > 1997 1.00 0 0 110
> > 1997 0.58 0 0 0
> > 1997 1.00 0 0 0
> > 1994 1.00 0 0 0
> > 1997 1.00 0 40 70
> > 1998 1.00 0 0 1252
> > 1999 1.04 0 74 0
> > 1999 1.00 0 0 0
> > 1999 1.02 0 0 0
> > 1999 1.00 0 0 0
> > 1999 1.00 0 0 171
> > 1999 1.00 1794 0 229
> > 1999 1.00 0 3525 0
> > 1997 1.00 1335 1185 147
> > 1997 1.00 4925 1057 4801
> > 1997 1.00 0 6275 1773
> >
> > I try to get sum("Total") by "Year" in which Tus>0, sum("Total") by
> "Year" in which Whi>0,,,and so on.
> >
> > I have done something like this;
> >
> > a<-as.list(numeric(3))
> > for (i in 3:5)
> > {
> > a[[i]]<-aggregate(frame[,"Total"],list(Year=frame$"Year",
> >
> Tus=frame$"i">0),sum)
> > }
> >
> >
> > The result is something like;
> >
> > Year Tus x
> > 1994 FALSE 49.69
> > 1995 FALSE 49.35
> > 1996 FALSE 56.95
> > 1997 FALSE 57.00
> > 1998 FALSE 57.00
> > 1999 FALSE 58.09
> > 2000 FALSE 56.97
> > 2001 FALSE 57.95
> > 2002 FALSE 57.10
> > 2003 FALSE 56.16
> > 2000 TRUE 1.00
> > 2002 TRUE 1.00
> > 2003 TRUE 2.01
> >
> >
> > But when I try indexing frame["Tus"==TRUE,],,,,I just don't get it
> >
> > Thank you
> >
> >
> > Luis Ridao Cruz
> > Fiskirannsˇknarstovan
> > Nˇat˙n 1
> > P.O. Box 3051
> > FR-110 Tˇrshavn
> > Faroe Islands
> > Phone: +298 353900
> > Phone(direct): +298 353912
> > Mobile: +298 580800
> > Fax: +298 353901
> > E-mail: luisr@frs.fo
> > Web: www.frs.fo
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
> > Luis Ridao Cruz
> > Fiskirannsˇknarstovan
> > Nˇat˙n 1
> > P.O. Box 3051
> > FR-110 Tˇrshavn
> > Faroe Islands
> > Phone: +298 353900
> > Phone(direct): +298 353912
> > Mobile: +298 580800
> > Fax: +298 353901
> > E-mail: luisr@frs.fo
> > Web: www.frs.fo
> >
> > ______________________________________________
>
[[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Wed Jul 28 02:04:27 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:55:17 EST