使用dplyr将函数应用于data.frame中的一行 [英] Apply function to a row in a data.frame using dplyr
问题描述
在基本 R
中,我将执行以下操作:
In base R
I would do the following:
d <- data.frame(a = 1:4, b = 4:1, c = 2:5)
apply(d, 1, which.max)
使用 dplyr
,我可以执行以下操作:
With dplyr
I could do the following:
library(dplyr)
d %>% mutate(u = purrr::pmap_int(list(a, b, c), function(...) which.max(c(...))))
如果 d
中还有另一列,我需要指定它,但是我希望它在任意数量的列中都可以工作.
If there’s another column in d
I need to specify it, but I want this to work w/ an arbitrary amount if columns.
从概念上讲,我想要类似的东西
Conceptually, I’d like something like
pmap_int(list(everything()), ...)
pmap_int(list(.), ...)
但这显然行不通.我怎么用 dplyr
规范地解决这个问题?
But this does obviously not work. How would I solve that canonically with dplyr
?
推荐答案
我们只需要将数据指定为.
,因为 data.frame
是list
,其中以列作为列表元素.如果包装 list(.)
,它将成为嵌套列表
We just need the data to be specified as .
as data.frame
is a list
with columns as list elements. If we wrap list(.)
, it becomes a nested 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
内,因为 list(everything())
不能解决应该从中选择所有内容的数据
Or if we want to use everything()
, place that inside select
as list(everything())
doesn't address the data from which everything should be selected
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
或使用折叠
library(collapse)
dapply(d, which.max, MARGIN = 1)
#[1] 2 2 3 3
可以作为
包含在 dplyr
中
d %>%
mutate(u = max.col(cur_data(), 'first'))
这篇关于使用dplyr将函数应用于data.frame中的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!