[R] Specify a minimum number of valid arguments for the mean function

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Sep 14 12:05:15 CEST 2010



On 13.09.2010 17:43, Luana Marotta wrote:
> Hello all,
>
> I want to specify a minimum number of valid arguments for the mean
> function--I have 5 variables but I want the mean only of cases that have at
> least 3 valid answers. What is the best way to do that?


If your 5 variables are in a data.frame dat:

sapply(dat, function(x) if(sum(!is.na(x)) > 2) mean(x) else NA)

Uwe Ligges


> Thank you very much!
>
> Luana
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



More information about the R-help mailing list