我们只需要将数据指定为.
as data.frame
is a list
以列作为列表元素。如果我们包裹list(.)
,它变成一个嵌套列表
library(dplyr)
d %>%
mutate(u = pmap_int(., ~ which.max(c(...))))
# a b c u
#1 1 4 2 2
#2 2 3 3 2
#3 3 2 4 3
#4 4 1 5 3
或者可以使用cur_data()
d %>%
mutate(u = pmap_int(cur_data(), ~ which.max(c(...))))
或者如果我们想使用everything()
,把它放在里面select
as list(everything())
不涉及应从中选择所有内容的数据
d %>%
mutate(u = pmap_int(select(., everything()), ~ which.max(c(...))))
或者使用rowwise
d %>%
rowwise %>%
mutate(u = which.max(cur_data())) %>%
ungroup
# A tibble: 4 x 4
# a b c u
# <int> <int> <int> <int>
#1 1 4 2 2
#2 2 3 3 2
#3 3 2 4 3
#4 4 1 5 3
或者这更有效max.col
max.col(d, 'first')
#[1] 2 2 3 3
Or with collapse
library(collapse)
dapply(d, which.max, MARGIN = 1)
#[1] 2 2 3 3
可以包含在dplyr
as
d %>%
mutate(u = max.col(cur_data(), 'first'))