[R] Getting group size in a data frame [Broadcast]

Liaw, Andy andy_liaw at merck.com
Thu Oct 19 14:32:35 CEST 2006


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}}



More information about the R-help mailing list