Sulz
Sulz

Reputation: 457

Extract all dataframes from a list which contains certain columns

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)

Upvotes: 3

Views: 86

Answers (4)

margusl
margusl

Reputation: 17594

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

Upvotes: 1

Mohamed Desouky
Mohamed Desouky

Reputation: 4425

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

Upvotes: 4

Andrea Barghetti
Andrea Barghetti

Reputation: 126

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]

Upvotes: 0

Rodrigo Lustosa
Rodrigo Lustosa

Reputation: 76

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]

Upvotes: 0

Related Questions