[R] Using a mathematical expression in sapply() XXXX

R. Michael Weylandt <michael.weylandt@gmail.com> michael.weylandt at gmail.com
Wed Jan 4 15:01:53 CET 2012



On Jan 4, 2012, at 7:41 AM, Dan Abner <dan.abner99 at gmail.com> wrote:

> Hello everyone,
> 
> I have the following call to sapply() and error message. Is the most
> efficient way to deal with this to make sum(!is.na(x)) a function in a
> separate line prior to this call?

Yes or inline using an anonymous/lambda function:

sapply(XX, function(x) sum(!is.na(x)))

> If not, please advise.
> 
> N.Valid=sapply(x,sum(!is.na(x)))
> Error in match.fun(FUN) :
>  'sum(!is.na(x))' is not a function, character or symbol
> 
> 
> Thanks!
> 
> Dan
> 
>    [[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