ry19
ry19

Reputation: 21

Using a loop in filter

I am quite new to R. Using dplyr and filter, I want to select records for which a list of variables !=NA.

df %>% filter (var1 != "NA" | var2 != "NA" | var3 != "NA" )

The problem is that I have 85 such variables (ending with HR). So I have extracted them and put them in a vector.

hr_variables <- grep("HR$", names(ssc), value=TRUE)

I would like to make a loop that will fetch hr_variable and then filter() by applying the OR condition to each element. Is this possible in R?

Upvotes: 1

Views: 649

Answers (1)

akrun
akrun

Reputation: 887971

We can use base R to do this more easily

ssc[!rowSums(is.na(ssc[hr_variables])),]
#  col1_HR col2_HR      col3
#2       1       3 0.5365853
#3       2       4 0.4196231

Or using tidyverse

library(tidyverse)
ssc %>% 
   select_(.dots = hr_variables) %>%
   map(~is.na(.)) %>% 
   reduce(`|`) %>%
    `!` %>% 
   extract(ssc, .,)

Or with complete.cases

ssc %>% 
     select_(.dots = hr_variables) %>% 
     complete.cases(.) %>% 
     extract(ssc, ., )

data

set.seed(24)
ssc <- data.frame(col1_HR = c(NA, 1, 2, 3), col2_HR = c(NA, 3, 4, NA), col3 = rnorm(4))

Upvotes: 1

Related Questions