我正在尝试使用map2来比较嵌套的tibble列。这是我的数据格式:
> tbl
# A tibble: 3 x 3
ID data.x data.y
<chr> <list> <list>
1 a <tibble [2 x 2]> <tibble [2 x 2]>
2 b <tibble [2 x 2]> <tibble [2 x 2]>
3 c <tibble [2 x 2]> <tibble [2 x 2]>
从列名的角度来看,data.x中的数据和data.y是相同的,值可能不同。我想从val列获得最大价值。我认为这可行,但只返回data.x的max。我并不完全了解map2的工作原理。
tbl %>%
mutate(col1 = map2_dbl(data.x, data.y, ~ max(.$val)))
结果应为:
# A tibble: 3 x 4
ID data.x data.y col1
<chr> <list> <list> <dbl>
1 a <tibble [2 x 2]> <tibble [2 x 2]> 7.
2 b <tibble [2 x 2]> <tibble [2 x 2]> 8.
3 c <tibble [2 x 2]> <tibble [2 x 2]> 8.
数据:
> dput(tbl)
structure(list(ID = c("a", "b", "c"), data.x = list(structure(list(
text = c("Y", "Y"), val = c(1, 1)), .Names = c("text", "val"
), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(text = c("N", "N"), val = c(2, 2)), .Names = c("text",
"val"), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
)), structure(list(text = c("Y", "Y"), val = c(3, 3)), .Names = c("text",
"val"), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))), data.y = list(structure(list(text = c("Y", "Y"), val = c(6,
7)), .Names = c("text", "val"), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(text = c("Y", "Y"), val = c(8,
6)), .Names = c("text", "val"), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame")), structure(list(text = c("N", "N"), val = c(7,
8)), .Names = c("text", "val"), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame")))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .Names = c("ID", "data.x", "data.y"
))
答案 0 :(得分:4)
根据预期的输出,我们正在提取“&#39; val&#39;两个&#39; data.x&#39; data.frame
中的列和&#39; data.y&#39; lists
,将它连接在一起(c
)并获取max
值
tbl %>%
mutate(col1 = map2_dbl(data.x, data.y, ~ max(c(.x$val, .y$val))))
# A tibble: 3 x 4
# ID data.x data.y col1
# <chr> <list> <list> <dbl>
#1 a <tibble [2 x 2]> <tibble [2 x 2]> 7.00
#2 b <tibble [2 x 2]> <tibble [2 x 2]> 8.00
#3 c <tibble [2 x 2]> <tibble [2 x 2]> 8.00
对于多列,可以使用&#39;数据&#39;,pmap
tbl %>%
mutate(col1 = pmap_dbl(.[-1], ~ max(c(..1$val, ..2$val))))