Dear R-Users, I have a big data set "mydata" with repeated observation and some missing values. It looks like the format below: userid sex item score1 score2 1 0 1 1 1 1 0 2 0 1 1 0 3 NA 1 1 0 4 1 0 2 1 1 0 1 2 1 2 NA 1 2 1 3 1 NA 2 1 4 NA 0 3 0 1 1 0 3 0 2 1 NA 3 0 3 1 0 3 0 4 0 0 I wound like to summarise the dataset such that i get something in the format of userid sumscore1 countscore1 meanscore1 sumscore2 countscore2 meanscore2 1 2 3 0.67 3 4 0.75 2 1 2 0.5 2 3 0.67 3 3 4 0.75 0 3 0.00 I tried using : means <- data.frame(aggregate(mydata[, 4:5],by=list(mydata$userid),FUN="mean", na.rm="TRUE")) and sums <- data.frame(aggregate(mydata[, 4:5],by=list(mydata$userid),FUN="sum", na.rm="TRUE")) so that i could merge the two data.frames later. This works quite okay but i still can not get a function that can give me a data.frame for the counts!! Something like this:: counts <- data.frame(aggregate(mydata[, 4:5],by=list(mydata$userid),FUN="count", na.rm="TRUE")). Any advice? Trevor Belgium -- View this message in context: http://n4.nabble.com/score-counts-in-an-aggregate-function-tp2007152p2007152.html Sent from the R help mailing list archive at Nabble.com.
Dear r-list, I have a big data set "mydata" with repeated observation and some missing values. It looks like the format below: userid sex item score1 score2 1 0 1 1 1 1 0 2 0 1 1 0 3 NA 1 1 0 4 1 0 2 1 1 0 1 2 1 2 NA 1 2 1 3 1 NA 2 1 4 NA 0 3 0 1 1 0 3 0 2 1 NA 3 0 3 1 0 3 0 4 0 0 I wound like to summarise the dataset such that i get something in the format of userid sumscore1 countscore1 meanscore1 sumscore2 countscore2 meanscore2 1 2 3 0.67 3 4 0.75 2 1 2 0.5 2 3 0.67 3 3 4 0.75 0 3 0.00 I tried using : means <- data.frame(aggregate(mydata[, 4:5],by=list(mydata$userid),FUN="mean", na.rm="TRUE")) and sums <- data.frame(aggregate(mydata[, 4:5],by=list(mydata$userid),FUN="sum", na.rm="TRUE")) so that i could merge the two data.frames later. This works quite okay but i still can not get a function that can give me a data.frame for the counts!! Something like this:: counts <- data.frame(aggregate(mydata[, 4:5],by=list(mydata$userid),FUN="* count*", na.rm="TRUE")). Any advice? Trevor Belgium -- Nice&Lovely [[alternative HTML version deleted]]
Hi Trever, You can do it like this: count <- function(x) { length(na.omit(x)) } counts <- data.frame(aggregate(mydata[,4:5],by=list(mydata$userid),FUN="count")) -Ista On Fri, Apr 16, 2010 at 10:35 AM, KDT <dkadengye@gmail.com> wrote:> > Dear R-Users, > I have a big data set "mydata" with repeated observation and some missing > values. It looks like the format below: > > userid sex item score1 score2 > 1 0 1 1 1 > 1 0 2 0 1 > 1 0 3 NA 1 > 1 0 4 1 0 > 2 1 1 0 1 > 2 1 2 NA 1 > 2 1 3 1 NA > 2 1 4 NA 0 > 3 0 1 1 0 > 3 0 2 1 NA > 3 0 3 1 0 > 3 0 4 0 0 > > I wound like to summarise the dataset such that i get something in the > format of > > userid sumscore1 countscore1 meanscore1 sumscore2 countscore2 > meanscore2 > 1 2 3 0.67 3 > 4 0.75 > 2 1 2 0.5 2 > 3 0.67 > 3 3 4 0.75 0 > 3 0.00 > > I tried using : > means <- data.frame(aggregate(mydata[, > 4:5],by=list(mydata$userid),FUN="mean", na.rm="TRUE")) > and > sums <- data.frame(aggregate(mydata[, > 4:5],by=list(mydata$userid),FUN="sum", > na.rm="TRUE")) > > so that i could merge the two data.frames later. This works quite okay but > i > still can not get a function that can give me a data.frame for the counts!! > Something like this:: > counts <- data.frame(aggregate(mydata[, > 4:5],by=list(mydata$userid),FUN="count", na.rm="TRUE")). > > Any advice? > > Trevor > Belgium > > -- > View this message in context: > http://n4.nabble.com/score-counts-in-an-aggregate-function-tp2007152p2007152.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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org [[alternative HTML version deleted]]
Thanks Ista Works well Trevor -- View this message in context: http://n4.nabble.com/score-counts-in-an-aggregate-function-tp2007152p2011057.html Sent from the R help mailing list archive at Nabble.com.
On Apr 16, 2010, at 11:16 AM, KDT wrote:> > Thanks Ista > > Works wellYou might also get further value from reviewing the various describe functions (there must be 5 or six packages with different versions). The Hmisc::descibe works very well for me and would have given you tabular counts for discrete variables and means, and quantiles for numerics. I also suspect that the data.frame(...) call around aggregate was superfluous, since the help page for aggregate says that its dataframe method returns a dataframe.> > Trevor > -- >-- David Winsemius, MD West Hartford, CT