zoowalk
zoowalk

Reputation: 2134

filtering nested dataframe (list column) over values in list with purrr

I have a dataframe including a list column. I want to filter this (nested) list column (data, containing a dataframe) on its contained column unit with the values contained in another list (x). I think I am quite close, the problem is I don't succeed to 'convert' list x into a vector for the filter statement. Greatful for any hint!

library(tidyverse)

the dataframe:

df<- structure(list(data = list(structure(list(unit = c("A1", "A2"
), value = c("10", "10")), class = c("tbl_df", "tbl", "data.frame"
), .Names = c("unit", "value"), row.names = c(NA, -2L)), structure(list(
  unit = c("B1", "B2", "A1"), value = c("10", "10", "10")), class = c("tbl_df", 
                                                                      "tbl", "data.frame"), .Names = c("unit", "value"), row.names = c(NA, 
                                                                                                                                       -3L)), structure(list(unit = c("C1", "B2"), value = c("10", "10"
                                                                                                                                       )), class = c("tbl_df", "tbl", "data.frame"), .Names = c("unit", 
                                                                                                                                                                            "value"), row.names = c(NA, -2L))), x = list(c("A1", "A2"), c("B1", 
                                                                                                                                                                                                                                                              "B2"), "C1")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                     -3L), .Names = c("data", "x"))

This works only if x has only one element:

df1 <- df %>% 
  mutate(y=map(data, ~filter(., unit %in% x)))

flatten_chr creates a vector including the values contained in x of all (!) rows, not for list per row.

df1 <- df %>% 
  mutate(y=map(data, ~filter(., unit %in% flatten_chr(x))))

The critical issues seems to be how can I convert x into a vector per row.

Upvotes: 1

Views: 1131

Answers (1)

Nate
Nate

Reputation: 10671

You can use map2() instead to iterate over data and x in parallel, i.e. row-wise.

df %>%
  mutate(y= map2(data, x, ~ filter(..1, unit %in% ..2))) # using ..1/..2 instead of .x/.y, to avoid confusion 

# A tibble: 3 x 3
  data             x         y               
  <list>           <list>    <list>          
1 <tibble [2 × 2]> <chr [2]> <tibble [2 × 2]>
2 <tibble [3 × 2]> <chr [2]> <tibble [2 × 2]>
3 <tibble [2 × 2]> <chr [1]> <tibble [1 × 2]>

In this pattern you don't need flatten_chr() anymore since x/..2 is already a character object inside of the mapping function.

Upvotes: 1

Related Questions