r - filter tibble of tibbles by nrow -


i've tibble that

>dat # tibble: 556 × 3            sample      run                   abc             <chr>    <chr>                <list> 1  206_03_07_2013 21102016   <tibble [304 × 21]> 2  206_04_07_2017  7082017   <tibble [229 × 21]> 3  206_04_10_2015 25112015 <tibble [2,687 × 21]> 4  206_07_08_2013 15102015   <tibble [460 × 21]> 5  206_08_12_2016  3032017 <tibble [3,250 × 21]> 6  206_11_03_2014 21102016   <tibble [975 × 21]> 7  206_13_02_2013 21112016   <tibble [101 × 21]> 8  206_13_03_2013 21112016   <tibble [345 × 21]> 9  206_14_08_2014  8092016 <tibble [1,952 × 21]> 10 206_19_03_2015 25012016    <tibble [11 × 21]> # ... 546 more rows 

the abc column contains tibble of different length. want filter dat tibble using length (>100 rows).

i :

dat[sapply(dat$abs,nrow)>100,] 

but use dplyr phylosophy ? ideas ?

thanks

a way be:

library(dplyr) library(purrr)  dat <- tribble(   ~foo, ~bar,   1, as_tibble(head(iris, 3)),   2, as_tibble(head(iris, 7)) )  # # tibble: 2 x 2 #     foo              bar #   <dbl>           <list> # 1     1 <tibble [3 x 5]> # 2     2 <tibble [7 x 5]>  res <- filter(dat, map_int(bar, nrow) > 5)  # # tibble: 1 x 2 #     foo              bar #   <dbl>           <list> # 1     2 <tibble [7 x 5]>  desired_output <- dat[sapply(dat$bar,nrow)>5,] identical(res, desired_output) # [1] true 

there not added value here, compared tried, it's matter of using drop-in replacements [ , sapply (with filter , map_int respectively). base r functions not incompatible so-called "dplyr philosophy". if mean use of magrittr pipe %>%, dat %>% .[sapply(.$bar, nrow) > 5, ] , dat %>% filter(map_int(bar, nrow) > 5) work equally well.

note: prefer all.equal on identical couldn't make work:

all.equal(res, desired_output) # error in equal_data_frame(target, current, ignore_col_order = ignore_col_order,  :  #   can't join on 'bar' x 'bar' because of incompatible types (list / list) 

(see https://github.com/tidyverse/dplyr/issues/2194)


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -