Le mercredi 04 janvier 2012 à 08:41 -0500, Dan Abner a écrit :
> 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? 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
You can use this:
sapply(x, function(x) sum(!is.na(x)))

But, if you can convert x to a matrix, it would be faster and shorter to
check for NAs beforehand, and use apply():
x <- matrix(c(1, 2, NA, 3, NA, 4), 2)
apply(!is.na(x), 2, sum)


Regards

______________________________________________
R-help@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.

Reply via email to