[R] apply mean function to a subset of data

Boris Steipe boris.steipe at utoronto.ca
Sat Apr 2 21:48:30 CEST 2016


Your toy code does not reproduce what you describe: mean(toy$diam[1:nsel[i]]) both times selects from elements of group 1. YOu probably want to subset like toy$diam[toy$group == i]. Also, if there is any real inefficiency here, it is _not_ because you are executing a for-loop for two iterations. What makes you think you have an efficiency problem?


B.

On Apr 2, 2016, at 2:46 PM, Pedro Mardones <mardones.p at gmail.com> wrote:

> 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]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list