I'm want to use map() of purrr package to apply filter() function to the data stored in a nested data frame.
Below is my code, where I would filter and then nest it.
I have achieved my desired result by performing nest() twice i.e. once on all the complete data and the second time on filtered data.
library(tidyverse)
df <- tibble(
x = sample(x = rep(c('a','b'),5), size = 10),
y = sample(c(1:10)),
z = sample(c(91:100))
)
#First time nested
df_fully_nested_dataframe <- df %>%
group_by(x) %>%
nest(.key = 'full')
#Second time nested
df_filtered_nested_dataframe <- df %>%
filter(z >= 95) %>%
group_by(x) %>%
nest(.key = 'filtered')
#The desired outcome is a single data frame with 2 nested list-columns: one full data and other with filtered data.
## Now how to achieve this without dividing into 2 dataframes
df_nested <- df_fully_nested_dataframe %>%
left_join(df_filtered_nested_dataframe, by = 'x')
The objects looks as follows:
> df
# A tibble: 10 x 3
x y z
<chr> <int> <int>
1 b 8 93
2 a 9 94
3 b 10 99
4 a 5 97
5 b 2 100
6 b 3 95
7 a 7 96
8 b 6 92
9 a 4 91
10 a 1 98
> df_fully_nested_dataframe
# A tibble: 2 x 2
x full
<chr> <list>
1 b <tibble [5 x 2]>
2 a <tibble [5 x 2]>
> df_filtered_nested_dataframe
# A tibble: 2 x 2
x filtered
<chr> <list>
1 b <tibble [3 x 2]>
2 a <tibble [3 x 2]>
> df_nested
# A tibble: 2 x 3
x full filtered
<chr> <list> <list>
1 b <tibble [5 x 2]> <tibble [4 x 2]>
2 a <tibble [5 x 2]> <tibble [4 x 2]>