Last active
December 31, 2019 06:42
-
-
Save rhilfi/8b28c0531d88dbfeafe297c3f0facc1d to your computer and use it in GitHub Desktop.
keep rows with at least one non-missing value #
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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