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,130 points
4,728 views

1 answer to this question.

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 Sahiti
• 6,370 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 17, 2018 in Data Analytics by DataKing99
• 8,250 points
1,917 views
0 votes
1 answer

How to use plyr and dplyr functions inside one source file?

Hi, Lakshmi, Use dplyr::function( ) or plyr::function( ). Since ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
716 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,090 points
1,607 views
+1 vote
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,720 points
1,705 views
0 votes
1 answer
+1 vote
5 answers

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

Try this: df %>% filter(!is.na(col1)) READ MORE

answered Mar 26, 2019 in Data Analytics by anonymous
332,146 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,090 points
1,103 views
0 votes
1 answer

How to apply list to a function which give data frame as output

If you use  tidyverse, you can use ...READ MORE

answered Apr 11, 2018 in Data Analytics by Sahiti
• 6,370 points
846 views
0 votes
2 answers

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

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,788 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP