I'm modifying nested data frames inside of foo
with map2
and mutate
, and I'd like to name a variable in each nested data frame according to foo$name
. I'm not sure what the proper syntax for nse
/tidyeval
unquotation would be here.
My attempt:
library(tidyverse)
foo <- mtcars %>%
group_by(gear) %>%
nest %>%
mutate(name = c("one", "two", "three")) %>%
mutate(data = map2(data, name, ~
mutate(.x, !!(.y) := "anything")))
#> Error in quos(...): object '.y' not found
I want the name of the newly created variable inside the nested data frames to be "one", "two", and "three", respectively.
I'm basing my attempt off the normal syntax I'd use if I was doing a normal mutate
on a normal df
, and where name
is a string:
name <- "test"
mtcars %>% mutate(!!name := "anything") # works fine
If successful, the following line should return TRUE
:
foo[1,2] %>% unnest %>% names %>% .[11] == "one"