我需要做的是将嵌套列表“a”第四层级别中所有
type
向量的值替换为相应的 transcodification
表中的值,并保持其余部分的相同结构:a = list(
a1 = list(
b1 = list(
c1 = list(
type = c(1,3),
attribute1 = runif(3,0,1),
attribute2 = list(d = rpois(1,1))
),
c2 = list(
type = c(2,3,6),
attribute1 = runif(3,0,1),
attribute2 = list(d = rpois(1,1))
)
),
b2 = list("foo")
),
a2 = list(
b1 = list(
c3 = list(
type = c(5),
attribute1 = runif(3,0,1),
attribute2 = list(d = rpois(1,1))
),
c4 = list(
type = c(2,3,6),
attribute1 = runif(3,0,1),
attribute2 = list(d = rpois(1,1))
)
),
b2 = list("foo")
),
a3 = list(
b1 = list(
c5 = list(
type = c(6),
attribute1 = runif(3,0,1),
attribute2 = list(d = rpois(1,1))
),
c6 = list(
type = c(1,2,3,5),
attribute1 = runif(3,0,1),
attribute2 = list(d = rpois(1,1))
)
),
b2 = list("foo")
)
)
transcodification = tibble(origin = c(1,2,3,4,5,6),
replacement = c("Peter","Jake","Matthew","Suzan","Christina","Margot"))
使用purrr
函数,这是否有可能实现?
b2 = list()
为b2 = runif(1,1,2)
来得到这个结果。任何你提供的解决方案都无法处理这个新列表。我得到了一个错误: Error: Can't coerce element 1 from a character to a double.需要帮助吗? - Fabien Pomponiomodify_depth(a, 3, ~modify_at(., 1, ~str_replace_all(., transcodification %>% pull(2) %>% set_names(1:length(.)))))
对我有效。 - Roman