Message-ID: <644e1f320903131617qffaa761n5e43c1c294c1b3cd@mail.gmail.com>
Date: 2009-03-13T23:17:57Z
From: jim holtman
Subject: Question on summing rows within nested variable
In-Reply-To: <72699C625F41984CB9386730055200A814E6C3B356@EXCH-VN01.sph.ad.jhsph.edu>
If you want zero if only one variable:
> DF2
sid pid slope
1 1.1 1.1 2
2 1.1 4.1 3
3 1.1 5.1 2
4 2.1 5.1 3
5 3.2 1.2 2
6 3.2 1.7 3
> tapply(DF2$slope, DF2$sid, function(x) if(length(x) == 1) 0 else mean(x))
1.1 2.1 3.2
2.333333 0.000000 2.500000
>
On Fri, Mar 13, 2009 at 6:43 PM, Vedula, Satyanarayana
<svedula at jhsph.edu> wrote:
> Hi,
>
> I was hoping someone could help figure out how to write code for R to do the below.
>
> I have data that looks like below. Variables, sid and pid are strings, slope is numeric. I need R to get me the mean of slopes for all pid's nested within each sid if there are more than one pid's nested within sid.
>
> If there is only pid for a sid, like for 2.1 below, I want R to write a 0.
>
> In the data below, I want to get the mean of slopes for pid (1.1; 4.1; and 5.1) because they are nested within sid 1.1 and so on.
>
> Thanks in advance for any suggestions.
> Swaroop
>
> sid ? ? ? ?pid ? ? ? ?slope
> 1.1 ? ? ? ?1.1 ? ? ? ?2
> 1.1 ? ? ? ?4.1 ? ? ? ?3
> 1.1 ? ? ? ?5.1 ? ? ? ?2
> 2.1 ? ? ? ?5.1 ? ? ? ?3
> 3.2 ? ? ? ?1.2 ? ? ? ?2
> 3.2 ? ? ? ?1.7 ? ? ? ?3
>
>
>
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?