Hello, I have a data set that looks like this. ID value 111 5 111 6 111 2 178 7 178 3 138 3 138 8 138 7 138 6 . . . I'd like to calculate the mean and var for each object identified by the ID. I can in theory just loop through the whole thing..., but is there a easier way/command which let me calculate the mean/var by ID? Thanks, Julia -- View this message in context: http://www.nabble.com/Calculate-mean-var-by-ID-tp19440461p19440461.html Sent from the R help mailing list archive at Nabble.com.
Try: with(x, sapply(list(mean, var), function(x)tapply(value, ID, x))) On Thu, Sep 11, 2008 at 2:45 PM, liujb <liujulia7@yahoo.com> wrote:> > Hello, > > I have a data set that looks like this. > ID value > 111 5 > 111 6 > 111 2 > 178 7 > 178 3 > 138 3 > 138 8 > 138 7 > 138 6 > . > . > . > > I'd like to calculate the mean and var for each object identified by the > ID. > I can in theory just loop through the whole thing..., but is there a easier > way/command which let me calculate the mean/var by ID? > > Thanks, > Julia > -- > View this message in context: > http://www.nabble.com/Calculate-mean-var-by-ID-tp19440461p19440461.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
aggregate(value,list(ID=ID),mean) aggregate(value,list(ID=ID),var) --Adam On Thu, 11 Sep 2008, liujb wrote:> > Hello, > > I have a data set that looks like this. > ID value > 111 5 > 111 6 > 111 2 > 178 7 > 178 3 > 138 3 > 138 8 > 138 7 > 138 6 > . > . > . > > I'd like to calculate the mean and var for each object identified by the ID. > I can in theory just loop through the whole thing..., but is there a easier > way/command which let me calculate the mean/var by ID? > > Thanks, > Julia > -- > View this message in context: http://www.nabble.com/Calculate-mean-var-by-ID-tp19440461p19440461.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >
Dear Julia, Try also x=read.table(textConnection("ID value 111 5 111 6 111 2 178 7 178 3 138 3 138 8 138 7 138 6"),header=TRUE) closeAllConnections() attach(x) do.call(rbind,tapply(value,ID, function(x){ res=c(mean(x,na.rm=TRUE),var(x,na.rm=TRUE)) names(res)=c('Mean','Variance') res } ) ) HTH, Jorge On Thu, Sep 11, 2008 at 1:45 PM, liujb <liujulia7@yahoo.com> wrote:> > Hello, > > I have a data set that looks like this. > ID value > 111 5 > 111 6 > 111 2 > 178 7 > 178 3 > 138 3 > 138 8 > 138 7 > 138 6 > . > . > . > > I'd like to calculate the mean and var for each object identified by the > ID. > I can in theory just loop through the whole thing..., but is there a easier > way/command which let me calculate the mean/var by ID? > > Thanks, > Julia > -- > View this message in context: > http://www.nabble.com/Calculate-mean-var-by-ID-tp19440461p19440461.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]