Consider this simple example
mydata <- data_frame(group = c('a', 'a', 'a', 'b', 'b', 'b'),
x = c(1,2,3,5,6,7),
y = c(3,5,6,4,3,2))
# A tibble: 6 x 3
group x y
<chr> <dbl> <dbl>
1 a 1 3
2 a 2 5
3 a 3 6
4 b 5 4
5 b 6 3
6 b 7 2
Here, I want to nest by group, and multiply the corresponding list-column dataframe by a given constant ONLY IF the group is a.
Something like:
> mydata %>% group_by(group) %>%
> nest() %>%
> mutate(flipped_df = map_if(data, group %in% c('a'), ~.x*-1))
> # A tibble: 2 x 3
> group data flipped_df
> <chr> <list> <list>
> 1 a <tibble [3 x 2]> <data.frame [3 x 2]>
> 2 b <tibble [3 x 2]> <tibble [3 x 2]>
I dont understand why I dont have a tibble anymore here. Any ideas?
Thanks!