Select columns in a nested list of dataframes

Hi,

I have a list of dataframes and I would like to be able to select a subset of columns in all those dataframes. In my specific problem, all the dataframes have common headers but some have additional columns so something like bind_rows wont work.

I want to select only the columns im interested in before converting everything to a single dataframe. Can anyone help. Below is my attempt.

library(tidyverse)

mydf <- iris %>% 
  nest(data=-Species) %>% 
  select(-Species)

# I only want to select Petal.Length Petal.Width
select(mydf, map(. , ~ select(., Petal.Length, Petal.Width)))

Thanks

Hi @john.smith,
https://stackoverflow.com/questions/44852512/dplyrselect-of-nested-data-frame

suppressPackageStartupMessages(library(tidyverse))

mydf <- iris %>% 
  nest(data=!Species) %>% 
  select(-Species)
mydf
#> # A tibble: 3 x 1
#>   data             
#>   <list>           
#> 1 <tibble [50 x 4]>
#> 2 <tibble [50 x 4]>
#> 3 <tibble [50 x 4]>

# I only want to select Petal.Length and Petal.Width
mydf %>% 
  mutate(data = map(data, ~select(., Petal.Length, Petal.Width))) -> smaller.df
smaller.df
#> # A tibble: 3 x 1
#>   data             
#>   <list>           
#> 1 <tibble [50 x 2]>
#> 2 <tibble [50 x 2]>
#> 3 <tibble [50 x 2]>

Created on 2021-08-27 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.