Shahin
Shahin

Reputation: 1316

using dplyr pipe to remove empty columns in a list of dataframes

I have a list of dataframes:

DF

LIST <- list(df1 <- data.frame(col_a1 = c("a","b"), col_b2=c(NA,NA)), df2 <- data.frame(col_b2 = c("a","b"), col_b1=c(NA,NA)))

[[1]]
  col1 col2
1    a   NA
2    b   NA

[[2]]
  col2 col1
1    a   NA
2    b   NA

I would like to remove the columns where all entries are NA, and select the ones that contain "1" using pipes

The resulting dataframe list is

[[1]]
cola1
a
b

I would prefer to use pipes to streamline this. The following code is what I've been trying:

LIST %>%
   map(~ .x %>% select(contains("1") & !all(is.na(.))))

Upvotes: 3

Views: 637

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389265

You could use two select functions :

library(dplyr)
library(purrr)

LIST %>% map(~ .x %>% select(contains("1")) %>% select_if(!all(is.na(.))))


#[[1]]
#  col_a1
#1      a
#2      b

#[[2]]
#data frame with 0 columns and 2 rows

Using only one select function we can do :

LIST %>% map(~ .x %>% select_if(str_detect(names(.x), '1') & 
                               colSums(!is.na(.x)) > 0))

And similarly in base R :

lapply(LIST, function(x) x[colSums(!is.na(x)) > 0 & grepl('1', names(x))])

Upvotes: 4

Related Questions