Another possibility would be to use the aggregate function:-
> aggregate(myDf[, 2:3], myDf$ID, FUN = mean)
myDf.ID ratio1 ratio2
K K 0.500000 2.1
L L 1.400000 4.1
X X 2.500000 2.0
Y Y 3.333333 2.5
Z Z 2.500000 1.9
Rich.
mangosolutions
S-PLUS and R Training and Consulting
Tel +44 118 902 6617
Fax +44 118 902 6401
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Tim Hesterberg
Sent: 12 December 2005 22:50
To: Khan, Sohail
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] Averaging rows
Try the function groupMeans() from the S+Resample library.
Download info below.
That computes all group means; you could then subscript to get only
the rows that had repeated values of ID; e.g.
groupMeans(x)[ unique(x$ID[duplicated(x$ID)]), ]
>I have data frame as follows:
>
>ID ratio_1 ratio_2
>X 2.5 2.0
>Y 3.6 2.1
>Y 2.9 1.2
>Y 3.5 4.2
>Z 2.5 1.9
>K 0.5 2.1
>L 1.4 4.1
>
>I would like average values of columns ratio_1, ratio_2 for all the IDs
with multiple values, i.e. ID Y would have average ratio_1 = 3.3333,
ratio_2.5.
>I am a novice S_PLUS user, sorry for the trivial question. Thanks.
>
>Sohail Khan
>Scientific Programmer
>COLD SPRING HARBOR LABORATORY
>Genome Research Center
>500 Sunnyside Boulevard
>Woodbury, NY 11797
>(516)422-4076
========================================================
| Tim Hesterberg Research Scientist |
| timh@insightful.com Insightful Corp. |
| (206)802-2319 1700 Westlake Ave. N, Suite 500 |
| (206)283-8691 (fax) Seattle, WA 98109-3012, U.S.A. |
| www.insightful.com/Hesterberg |
========================================================
Download the S+Resample library from www.insightful.com/downloads/libraries
Two Research Scientist positions:
data mining
frailty/mixed effects
http://www.insightful.com/company/jobs.asp
Speak out about biased science in Washington D.C.
http://home.comcast.net/~timhesterberg/ScientificIntegrity.html
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|