Skip to content

Instantly share code, notes, and snippets.

@kongscn
Last active December 20, 2015 14:49
Show Gist options
  • Save kongscn/6149396 to your computer and use it in GitHub Desktop.
Save kongscn/6149396 to your computer and use it in GitHub Desktop.
Remove NA in R
# Reference:
# http://stackoverflow.com/questions/4862178/remove-rows-with-nas-in-data-frame
# example from ?na.omit, changed.
# use complete.cases to get a bool vector of cases without missing data.
DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z = c(NA, 10, 6))
DF[complete.cases(DF), ]
# this can be used to choose specific columns of interests
DF[complete.cases(DF[, 'y']),]
# to simplily omit all rows containing NA, use na.omit
na.omit(DF)
m <- as.matrix(DF)
na.omit(m)
options("na.action")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment