I've been working my way through the R purrr package, and I've come to a roadblock. I created some mock data below which represents a very small snippet of what my data actually looks like.
library(tidyverse)
my_data <- tribble(
~lookup_lists, ~old_vectors,
# Observation 1
list(
"X1" = "one",
"X7" = "two",
"X16" = "three"
),
c("Col1", "Col2", "Col3", "X1", "X7", "X16"),
# Observation 2
list(
"X3" = "one",
"X8" = "two",
"X22" = "three"
),
c("Col1", "Col2", "Col3", "X3", "X8", "X22")
)
At this point, I want to make a new column that has the same vector values as old_vectors
but the values that start with X are recoded to reflect the lookup named list in the lookup_lists
. For example, I want the first row to go from:
c("Col1", "Col2", "Col3", "X1", "X7", "X16")
to
c("Col1", "Col2", "Col3", "one", "two", "three")
and be saved to a new column in the nested tibble. Here is my attempt using the map2
function:
# Add a third column that has the recoded vectors
my_data <- my_data %>%
mutate(new_vectors = map2(.x = old_vectors, .y = lookup_lists, .f = ~recode(.x, .y)))
#> Error in mutate_impl(.data, dots): Evaluation error: Argument 2 must be named, not unnamed.
I don't understand this because the second argument IS named. Here is the first observation's lookup_list to show my point:
my_data$lookup_lists[[1]]
$X1
[1] "one"
$X7
[1] "two"
$X16
[1] "three"
I think I'm missing something pretty obvious, and probably has something to do with this. Any help would be greatly appreciated!