[R] How to include custom na.action in function

Alejandro a.gonzalezvoyer at gmail.com
Thu Feb 9 20:30:23 CET 2017


I’ve tried googling for an answer to this but I simply can’t find something that fixes my problem. I have a long numerical vector with positive, negative and null values. I want to revert the sign of the positive and negative values and for zero to remain zero. I’ve written a function that works, except that my vector has missing values (NA) and I need to keep those as missing values. How could I add that to this function:

 if (x > 0) {x <- x*-1}
if (x < 0) {x <- abs(x)}
   if (x == 0) {x <- 0}

I’ve tried if(is.na(x)) {x <- NA} but I get the following error message: Error in if (x > 0) { : missing value where TRUE/FALSE needed. Which I guess is the first NA in the vector which fails the first if of the function.

I use supply() to run the function on a vector.

Thanks for any assistance.



More information about the R-help mailing list