Hello I am trying to take a DF columns 13:19 and return all rows that contain at least one piece of data. DF could look like:
13 14 15... 19
cool not cool NA...
NA NA NA NA...
NA 1 NA NA...
NA NA NA NA...
and return just
13 14 15... 19
cool not cool NA...
NA 1 NA NA...
I have tried variants of:
df %>% select(c(13:19)) %>% !is.na()
na.omit(df[,c(13:19)])
df[,c(13:19)] %>%
select_if(~ !any(is.na(.)))
I would love to be able to do this with dplyr but cannot.
I think what I am passing is trying to parse TRUE/FALSE on all row data for all columns 13:19 to satisfy the condition of not NA or contains at least 1 or more values. Any help would be greatly appreciated - thank you.
CodePudding user response:
With dplyr
, we could look for any rows that do not have NA
s and then return only those rows.
library(dplyr)
df %>%
filter(if_any(13:19, ~ !is.na(.)))
Or you can use rowSums
:
df %>%
filter(rowSums(!is.na(df[, c(13:19)])) != 0)
Or in base R:
df[apply(!is.na(df[,13:19]),1,any),]
Or
df[rowSums(!is.na(df[,13:19])) != 0,]