# Re: [R] Getting group size in a data frame

From: Alex Brown <alex_at_transitive.com>
Date: Thu 19 Oct 2006 - 10:52:15 GMT

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@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

R-help@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. Received on Thu Oct 19 21:22:05 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 19 Oct 2006 - 11:30:13 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.