I have a data frame with a bunch of nested data-frames within it, and I'd like to apply dplyr::select to each of those nested data frames. Here's an example
library(tidyverse)
mtcars %>%
group_by(cyl) %>%
nest %>%
mutate(data2 = ~map(data, dplyr::select(.,-mpg)))
I would think that this would result in a data frame with three columns. cyl
: the number of cylinders, data
: the nested data, data2
: the same as data except each element would not have the mpg column.
Instead R crashes:
*** caught segfault *** address 0x7ffc1e445000, cause 'memory not mapped' Traceback: 1: .Call(`_dplyr_mutate_impl`, df, dots) 2: mutate_impl(.data, dots) 3: mutate.tbl_df(., data2 = ~map(data, dplyr::select(., -mpg))) 4: mutate(., data2 = ~map(data, dplyr::select(., -mpg))) 5: function_list[[k]](value) 6: withVisible(function_list[[k]](value)) 7: freduce(value, `_function_list`) 8: `_fseq`(`_lhs`) 9: eval(quote(`_fseq`(`_lhs`)), env, env) 10: eval(quote(`_fseq`(`_lhs`)), env, env) 11: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env)) 12: mtcars %>% group_by(cyl) %>% nest %>% mutate(data2 = ~map(data, dplyr::select(., -mpg))) Possible actions: 1: abort (with core dump, if enabled) 2: normal R exit 3: exit R without saving workspace 4: exit R saving workspace
I realize I could get the columns I wanted if I apply the select operation before the nesting, but this would be less analogous with my real problem. Could somebody please explain to me what I am doing wrong here? Thanks for any advice.
mutate(data = map(data, function(x) dplyr::select(x, -mpg)))
– Russ HydesessionInfo()
-output. – IRTFM