Skip to content

Instantly share code, notes, and snippets.

@rhilfi
Last active December 31, 2019 06:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhilfi/8b28c0531d88dbfeafe297c3f0facc1d to your computer and use it in GitHub Desktop.
Save rhilfi/8b28c0531d88dbfeafe297c3f0facc1d to your computer and use it in GitHub Desktop.
keep rows with at least one non-missing value #
# adapted from: https://stackoverflow.com/questions/44077122/r-select-rows-with-non-na-values-in-at-least-one-of-the-four-columns
a<-c(NA,NA,NA,NA,2,5,4,3,NA,NA)
b<-c(NA,NA,NA,NA,2,3,4,5,NA,NA)
c<-c(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)
d<-c(1,4,3,1,2,4,3,2,1,NA)
df<-cbind(a,b,c,d)
df_atLeastOneNonMissingValue <- df[rowSums(is.na(df)) != ncol(df),] ## just to keep rows with at least one non-missing value
nrow(df)
nrow(df_atLeastOneNonMissingValue)
# with selected rows:
df_atLeastOneNonMissingValueInFirstThreeColumns <- df[rowSums(is.na(df[, c("a","b","c")])) != 3,]
nrow(df_atLeastOneNonMissingValueInFirstThreeColumns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment