Skip to content

Instantly share code, notes, and snippets.

@ajdamico
Created June 28, 2012 01:22
Show Gist options
  • Save ajdamico/3008092 to your computer and use it in GitHub Desktop.
Save ajdamico/3008092 to your computer and use it in GitHub Desktop.
binary operators that never return missing values - how to remove NA values from logical tests
#create the remove NA function
no.na <-
function( x , value = FALSE ){
x[ is.na( x ) ] <- value
x
}
#full discussion
#http://r.789695.n4.nabble.com/binary-operators-that-never-return-missing-values-td4634024.html
#usage examples
#define two numeric vectors
a <- c( 1 , NA , 7 , 2 , NA , 4 )
b <- c( NA , NA , 9 , 1 , 6 , 4 )
a < b #straight, with NAs in results
no.na( a < b ) #with NAs defaulting to FALSE
no.na( a < b , TRUE ) #with NAs converted to TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment