I want to mutate a variable to a list column based on a filtered dataframe that is itself nested inside the list column.
Reprex: I use the built-in diamonds package
library(tidyverse)
play <-
diamonds %>%
gather(letters, value, x:z) %>%
nest(letters, value, .key = "nest2") %>%
group_by(cut) %>%
nest(.key = "nest1")
I now have a 5x2 tibble with a cut
column and nest1
which is the list column. Inside that are 6 normal variables and a further list column nest2
.
I want to mutate a column in nest1
with a count of rows in nest2
. I can do this with
play_2 <-
play %>%
mutate(nest1 = map(nest1, ~ mutate(.x, n_row = map_int(nest2, nrow))))
play_2$nest1[3] #to check
What actually want is a count of rows in nest2
based on a filter e.g. nest2 != "y"
. I have tried numerous subset options but am failing miserably. I am sure this is to do with the fact nest2
is a list of tibbles but I can't figure out the correct way to approach it.