Home > Software engineering >  Extract all dataframes from a list which contains certain columns
Extract all dataframes from a list which contains certain columns

Time:01-20

I have a list like:

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(23, 41, 32, 58, 26)
Hight <- c(13, 41, 32, 58, 26)
Weight <- c(11,43,23,43,123)

df1 <- data.frame(Name, Age, Hight, Weight)
df2 <- data.frame(Name, Age, Hight)
df3<- data.frame(Name, Age, Weight)

l <- list(df1, df2, df3)

I want now to extract all dataframes (or the name of the dataframes), which contain the Hight andWeight columns.

The expected output in this case would list(df1)

CodePudding user response:

You can use this

l <- list(df1, df2, df3)

l[sapply(l , \(x) all(c('Weight', 'Hight') %in% colnames(x)))]
  • output
[[1]]
   Name Age Hight Weight
1   Jon  23    13     11
2  Bill  41    41     43
3 Maria  32    32     23
4   Ben  58    58     43
5  Tina  26    26    123

CodePudding user response:

Purrr comes with keep() / discard():

dfs <- list(df1, df2, df3)

purrr::keep(dfs, ~ all(c("Hight", "Weight") %in% colnames(.x)))
#> [[1]]
#>    Name Age Hight Weight
#> 1   Jon  23    13     11
#> 2  Bill  41    41     43
#> 3 Maria  32    32     23
#> 4   Ben  58    58     43
#> 5  Tina  26    26    123

CodePudding user response:

One possible way is using a for loop:

dfs <- list(df1, df2, df3)

n <- length(dfs)
index <- NULL # index of dataframes to be used
for (i in 1:n){
  if ("Hight" %in% names(dfs[[i]]) & "Weight" %in%  names(dfs[[i]]))
    index <- c(index, i)
}

dfs[index]

CodePudding user response:

df_list <- list(df1, df2, df3)
have_all_columns <- purrr:::map_lgl(df_list, ~all(c( "Hight", "Weight") %in% names(.x)))
df_list[have_all_columns]
  • Related