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 Thanks! Dan [[alternative HTML version deleted]]
Milan Bouchet-Valat
2012-Jan-04 13:57 UTC
[R] Using a mathematical expression in sapply() XXXX
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 symbolYou 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. Michael Weylandt <michael.weylandt@gmail.com>
2012-Jan-04 14:01 UTC
[R] Using a mathematical expression in sapply() XXXX
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.
Meyners, Michael
2012-Jan-04 14:02 UTC
[R] Using a mathematical expression in sapply() XXXX
Dan, It depends on what you want to achieve. I suspect you just want to remove missing values before summing; if so, consider sapply(x, sum, na.rm=TRUE) instead. To make your code running, try sapply(x, function(x) sum(!is.na(x))) However, this would just count the number of non-missing values per subgroup, not the sum of the values. Similarly, sapply(x, function(x) sum(x, na.rm=TRUE)) would follow the same rationale but give the sums (with missing values removed), i.e. the same results as the first line of code. I'd rather choose the first option than this one, though. Of course, you could also define a new function sum1 <- function(x) sum(!is.na(x)) # or probably rather sum(x, na.rm=TRUE), depending on your needs and then use sapply(x, sum1) but that seems a bit overkill, I'd say... HTH, Michael> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Dan Abner > Sent: Wednesday, January 04, 2012 14:41 > To: r-help at r-project.org > Subject: [R] Using a mathematical expression in sapply() XXXX > > 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 > > > 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.
Maybe Matching Threads
- SAPPLY function XXXX
- Writing a function to return column position XXXX
- Convert components of a list to separate columns in a data frame or matrix XXXX
- Applyiing mode() or class() to each column of a data.frame XXXX
- Importing data from MS EXCEL (.xls) to R XXXX