Hi all, I have a data frame with some measured values of some animals. Sometimes the measurement failed, resulting in a NA for a measurement and sometimes the animal died, resulting in NA for all measurements. I have several groups of animals. How do I find the size of each group with only alive animals? And how do I find the size of the groups for each measurement? An example: l1 <- list(factor=c(24,24,24), val1=c(2, 3, NA), val2=c(4, NA, NA)) df <- as.data.frame(l1) df$factor <- factor(df$factor) The size of factors should be 2 and not 3. The number of measurement in val1 should be 2 and the number of measurements in val2 should be 1 Thanks in advance for any help and suggestions Ulrik [[alternative HTML version deleted]]
I can't quite tell what you are looking for, but try the following: measurecols = c("val1","val2") df2 <- df[!apply(is.na(df[1:nrow(df),measuredcols]),1,all),] to remove rows which have no measurements in. a simple count of the rows (nrow) will then give you the number of animals that didn't die, and then table(df2$factor) will tell you how many per group didn't die table(df2$factor[!is.na(df2$val1)] and names(measurecols) = measurecols lapply(measurecols, function(x)table(df2$factor[!is.na(df2[,x])])) will tell you for each measurement, how many of each group you got. -Alex On 19 Oct 2006, at 08:37, Ulrik Stervbo wrote:> Hi all, > > I have a data frame with some measured values of some animals. > Sometimes the > measurement failed, resulting in a NA for a measurement and > sometimes the > animal died, resulting in NA for all measurements. > > I have several groups of animals. How do I find the size of each > group with > only alive animals? And how do I find the size of the groups for each > measurement? > > An example: > l1 <- list(factor=c(24,24,24), val1=c(2, 3, NA), val2=c(4, NA, NA)) > df <- as.data.frame(l1) > df$factor <- factor(df$factor) > > The size of factors should be 2 and not 3. The number of > measurement in > val1 should be 2 and the number of measurements in val2 should be 1 > > Thanks in advance for any help and suggestions > Ulrik > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
Is this sort of what you want? R> aggregate(df[2:3], df[1], function(x) sum(!is.na(x))) factor val1 val2 1 24 2 1 Andy From: Ulrik Stervbo> > Hi all, > > I have a data frame with some measured values of some > animals. Sometimes the > measurement failed, resulting in a NA for a measurement and > sometimes the > animal died, resulting in NA for all measurements. > > I have several groups of animals. How do I find the size of > each group with > only alive animals? And how do I find the size of the groups for each > measurement? > > An example: > l1 <- list(factor=c(24,24,24), val1=c(2, 3, NA), val2=c(4, NA, NA)) > df <- as.data.frame(l1) > df$factor <- factor(df$factor) > > The size of factors should be 2 and not 3. The number of > measurement in > val1 should be 2 and the number of measurements in val2 should be 1 > > Thanks in advance for any help and suggestions > Ulrik > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}