Luana Marotta
2010-Sep-13 15:43 UTC
[R] Specify a minimum number of valid arguments for the mean function
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? Thank you very much! Luana [[alternative HTML version deleted]]
Uwe Ligges
2010-Sep-14 10:05 UTC
[R] Specify a minimum number of valid arguments for the mean function
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.