How do I select all shared columns within nested df in R using tidyverse functions?

Viewed 88

I've come up with a reprex below. I've created a nested df. The cols column represents columns within the nested df. I am using the cols column to pass to the made up function using map.

In this case, that would be the lifeExp column.

How do I pass along a select that looks at the cols column and selects the character vector column name that exists within that row, and then select that row in the sd_col nested df? In addition, I'd like to pass the country, continent and sd columns in that same select. So in this case, we would be selecting, country, continent, lifeExp, and sd within the nested df in the sd_col.

I'm guessing that this could be achieved using purrr.

library(gapminder)
library(tidyverse)

data <- gapminder_unfiltered


sample_fn1 <- function(data, cols){
  testdata <- data %>% 
    mutate(across(!!cols, mean))
}


sample_fn2 <- function(data, cols){
  testdata <- data %>% 
    mutate(sd = sd(pop))
}
nested_data <- data %>% 
  filter(country %in% c("United States", "Mexico", "Canada", "Argentina", "Brazil", "Italy", "Japan")) %>% 
  mutate(
    country_group = case_when(
    country %in% c("United States", "Mexico", "Canada") ~ "North America",
    country %in% c("Argentina", "Brazil") ~ "South America",
    country %in% c("Italy", "Japan") ~ "Eurasia"
  )
  ) %>% 
  group_by(country_group) %>% 
  nest() %>% 
  ungroup()


> nested_data 
# A tibble: 3 x 2
  country_group data                  
  <chr>         <list>                
1 South America <tibble[,6] [24 x 6]> 
2 North America <tibble[,6] [127 x 6]>
3 Eurasia       <tibble[,6] [114 x 6]>

Next

cols to use

col_tbl <- tibble::tribble(
  ~country_group, ~cols,
  "North America", c("lifeExp", "pop"),
  "South America", c("lifeExp", "gdpPercap"),
  "Eurasia"       , c("lifeExp", "pop")
)

> col_tbl
# A tibble: 3 x 2
  country_group cols     
  <chr>         <list>   
1 North America <chr [2]>
2 South America <chr [2]>
3 Eurasia       <chr [2]>

join cols to use to df

nested_data <- nested_data %>% 
  left_join(col_tbl)
#> Joining, by = "country_group"

> nested_data
# A tibble: 3 x 3
  country_group data                   cols     
  <chr>         <list>                 <list>   
1 South America <tibble[,6] [24 x 6]>  <chr [2]>
2 North America <tibble[,6] [127 x 6]> <chr [2]>
3 Eurasia       <tibble[,6] [114 x 6]> <chr [2]>

run 2 functions

nested_data <- nested_data %>% 
  mutate(nest1 = map2(data, cols, sample_fn1),
         nest2 = map(data, sample_fn2),
         # select specified columns 
         nest1 = map(nest1, ~select(.x, c(country, year, continent)))
         )

> nested_data
# A tibble: 3 x 5
  country_group data                   cols      nest1                  nest2                 
  <chr>         <list>                 <list>    <list>                 <list>                
1 South America <tibble[,6] [24 x 6]>  <chr [2]> <tibble[,3] [24 x 3]>  <tibble[,7] [24 x 7]> 
2 North America <tibble[,6] [127 x 6]> <chr [2]> <tibble[,3] [127 x 3]> <tibble[,7] [127 x 7]>
3 Eurasia       <tibble[,6] [114 x 6]> <chr [2]> <tibble[,3] [114 x 3]> <tibble[,7] [114 x 7]>
0 Answers
Related