hello, i want to compute the mean of a variable ("aps") for every class (1,2, and 3). every id have a few obs., "aps" and class are constant over id. like this: id aps class 1 11 2 1 11 2 1 11 2 1 11 2 1 11 2 2 8 3 2 8 3 2 8 3 3 12 2 3 12 2 . . i tried: tapply(icu1$aps_st, icu1$hidclass, function(z) mean(unique(z))) but it's counting every row and not every id. thank you, Sigalit. [[alternative HTML version deleted]]
I do not understand what you want. If aps is constant over each class then the mean for each class is equal to any value of aps. Using your example you can do tapply(icu1$aps, icu1$d, mean) but it does not give you anything new. Can you explain the problem a bit more? --- sigalit mangut-leiba <smangut at gmail.com> wrote:> hello, > i want to compute the mean of a variable ("aps") for > every class > (1,2, and 3). > every id have a few obs., "aps" and class are > constant over id. > like this: > id aps class > 1 11 2 > 1 11 2 > 1 11 2 > 1 11 2 > 1 11 2 > 2 8 3 > 2 8 3 > 2 8 3 > 3 12 2 > 3 12 2 > . > . > > i tried: > > tapply(icu1$aps_st, icu1$hidclass, function(z) > mean(unique(z))) > > but it's counting every row and not every id. > > thank you, > > Sigalit.
I also don't understand, but perhaps: with(df, tapply(aps, list(class, id), mean)) -- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O On 19/07/07, sigalit mangut-leiba <smangut at gmail.com> wrote:> hello, > i want to compute the mean of a variable ("aps") for every class > (1,2, and 3). > every id have a few obs., "aps" and class are constant over id. > like this: > id aps class > 1 11 2 > 1 11 2 > 1 11 2 > 1 11 2 > 1 11 2 > 2 8 3 > 2 8 3 > 2 8 3 > 3 12 2 > 3 12 2 > . > . > > i tried: > > tapply(icu1$aps_st, icu1$hidclass, function(z) mean(unique(z))) > > but it's counting every row and not every id. > > thank you, > > Sigalit. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
I'm sorry for the unfocused questions, i'm new here... the output should be: class aps_mean 1 na 2 11.5 3 8 the mean aps of every class, when every id count *once*, for example: class 2, mean= (11+12)/2=11.5 hope it's clearer. sigalit. [[alternative HTML version deleted]]