Message-ID: <CAEd7G2duUqevXy2d+A=aeL6ECH=+sQhEY5FTrUdsO_0yrNbA0w@mail.gmail.com>
Date: 2016-04-02T18:46:52Z
From: Pedro Mardones
Subject: apply mean function to a subset of data
Dear all;
This must have a rather simple answer but haven't been able to figure it
out: I have a data frame with say 2 groups (group 1 & 2). I want to select
from group 1 say "n" rows and calculate the mean; then select "m" rows from
group 2 and calculate the mean as well. So far I've been using a for loop
for doing it but when it comes to a large data set is rather inefficient.
Any hint to vectorize this would be appreciated.
toy = data.frame(group = c(rep(1,10),rep(2,8)), diam =
c(rnorm(10),rnorm(8)))
nsel = c(6,4)
smean <- c(0,0)
for (i in 1:2) smean[i] <- mean(toy$diam[1:nsel[i]])
Thanks
Pedro
[[alternative HTML version deleted]]