【发布时间】:2021-06-30 06:02:13
【问题描述】:
在基础R 中,我会执行以下操作:
d <- data.frame(a = 1:4, b = 4:1, c = 2:5)
apply(d, 1, which.max)
使用dplyr 我可以执行以下操作:
library(dplyr)
d %>% mutate(u = purrr::pmap_int(list(a, b, c), function(...) which.max(c(...))))
如果d 中有另一列,我需要指定它,但我希望它可以在任意数量的 if 列中工作。
从概念上讲,我想要类似的东西
pmap_int(list(everything()), ...)
pmap_int(list(.), ...)
但这显然行不通。我将如何使用dplyr 规范地解决这个问题?
【问题讨论】: