【发布时间】:2018-12-06 19:09:15
【问题描述】:
我一直在研究 R purrr 包,但遇到了障碍。我在下面创建了一些模拟数据,它们代表了我的数据实际外观的一个非常小的 sn-p。
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")
)
此时,我想创建一个与old_vectors 具有相同向量值但以 X 开头的值的新列,以反映@987654324 中的查找命名列表@。例如,我希望第一行来自:
c("Col1", "Col2", "Col3", "X1", "X7", "X16")
到
c("Col1", "Col2", "Col3", "one", "two", "three")
并保存到嵌套 tibble 中的新列。这是我使用map2 函数的尝试:
# 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.
我不明白这一点,因为第二个参数 IS 命名。这是第一个观察的 lookup_list 来说明我的观点:
my_data$lookup_lists[[1]]
$X1
[1] "one"
$X7
[1] "two"
$X16
[1] "three"
我认为我遗漏了一些非常明显的东西,可能与this 有关。任何帮助将不胜感激!
【问题讨论】:
标签: r dictionary purrr