Skip to content
Back to formatted view

Raw Message

Message-ID: <CA+8X3fWno6dwO1VKXRxbRGOUb2pRL_KjHnDUraDGVPctPUZANw@mail.gmail.com>
Date: 2016-04-02T23:13:57Z
From: Jim Lemon
Subject: apply mean function to a subset of data
In-Reply-To: <CAEd7G2duUqevXy2d+A=aeL6ECH=+sQhEY5FTrUdsO_0yrNbA0w@mail.gmail.com>

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)

Jim

On 4/3/16, 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.
>