How to use dplyr functions such as filter() inside nested data frames with map()

0 votes

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]>

Apr 6, 2018 in Data Analytics by nirvana
• 3,040 points
84 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use map() call as follows:

 map(full, ~ filter(., z >= 95))

where . stands for individual nested tibble, to which you can apply the filter directly

df_nested_again <- df_fully_nested_dataframe %>% mutate(filtered = map(full, ~ filter(., z >= 95)))

identical(df_nested, df_nested_again)
# [1] TRUE
answered Apr 6, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 16, 2018 in Data Analytics by DataKing99
• 8,100 points
40 views
0 votes
1 answer

How to join a list of data frames using map()

You can use reduce set.seed(24) r1 <- map(c(5, 10, ...READ MORE

answered Apr 11, 2018 in Data Analytics by kappa3010
• 2,010 points
20 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
31 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
31 views
0 votes
1 answer
0 votes
3 answers

How to remove NA values with dplyr::filter()

Null values have no notion of equality ...READ MORE

answered Apr 11 in Data Analytics by Zane
2,720 views
0 votes
1 answer
0 votes
1 answer

How can I use parallel so that it preserves the list of data frames

You can use pmap as follows: nc <- ...READ MORE

answered Apr 4, 2018 in Data Analytics by kappa3010
• 2,010 points
18 views
0 votes
1 answer
0 votes
1 answer

How to remove rows with missing values (NAs) in a data frame?

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
2,882 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.