Search code examples
rfilternest

How to apply the filter function for a variable which is stored in type list?


In test_data, there is a variable data stored in type list, how to filter the x variable value?

 library(tidyverse)
    test_data <- diamonds %>% nest(.by='cut')
    
    # below code can't work
    test_data %>% filter(test_data$data[[['x']]] >4)

Solution

  • Besides the possibilities for filtering of nested data frames as described in the question which is linked in the comment, I find it worth to mention that there is the nplyr package which contains nest_filter(), a convenient wrapper for dplyr::filter for filtering nested data frames.

    You can e.g. filter the x variable for values > 4 as given in your example by

    library(nplyr)
    
    nest_filter(test_data, # df 
                data, # column containing the nested data
                x > 4) # filter condition, can be extended to multiple conditions
    
    # A tibble: 5 × 2
      cut       data                 
      <ord>     <list>               
    1 Ideal     <tibble [21,449 × 9]>
    2 Premium   <tibble [13,752 × 9]>
    3 Good      <tibble [4,861 × 9]> 
    4 Very Good <tibble [11,776 × 9]>
    5 Fair      <tibble [1,606 × 9]>