Re: [R] apply mean function to a subset of data

2016-04-04 Thread Pedro Mardones
.117015 > > -- > David L. Carlson > Department of Anthropology > Texas A&M University > > -Original Message- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jim Lemon > Sent: Saturday, April 2, 2016 6:14 PM > To: Pedro Mar

Re: [R] apply mean function to a subset of data

2016-04-03 Thread David L Carlson
2016 6:14 PM To: Pedro Mardones Cc: r-help mailing list Subject: Re: [R] apply mean function to a subset of data Hi Pedro, This may not be much of an improvement, but it was a challenge. selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel), ncol=2,byrow=TRUE)) TFv

Re: [R] apply mean function to a subset of data

2016-04-02 Thread Jim Lemon
Hi Pedro, This may not be much of an improvement, but it was a challenge. selvec<-as.vector(matrix(c(nsel,unlist(by(toy$diam,toy$group,length))-nsel), ncol=2,byrow=TRUE)) TFvec<-rep(c(TRUE,FALSE),length.out=length(selvec)) toynsel<-rep(TFvec,selvec) by(toy[toynsel,]$diam,toy[toynsel,]$group,mean)

Re: [R] apply mean function to a subset of data

2016-04-02 Thread Boris Steipe
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.

[R] apply mean function to a subset of data

2016-04-02 Thread Pedro Mardones
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