R 矢量化和 dplyr 变异错误?

R vectorization and dplyr mutate bug?

我有一个简单的函数来计算矢量化和(在这个例子中我使用的是幂和,但我实际使用的是一个更复杂的函数)和一个 data.frame 来计算它。

mapply 可以很好地执行此操作,但是 dplyr::mutate 正在抛出警告消息:Warning message: In 1:n : numerical expression has 4 elements: only the first used。如果我先使用 rowwise mutate 会给我正确的结果。

如果您能深入了解正在发生的事情,我将不胜感激。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
powersum <- function(n, p) {
  i <- 1:n # this is the basic structure that I am using
  sum(i^p) # `i^p` is more complex in my real function with more variables
}

df <- data.frame(
  n = c(1:4),
  p = rep(3, 4)
)

mapply(powersum, df$n, df$p) # works great

library(dplyr)
df %>% mutate(powersum = powersum(n, p)) # throws warning about elements

df %>% rowwise() %>% mutate(powersum = powersum(n, p)) # works, but why?


如果我们需要tidyverse

中对应的选项

1
2
3
4
5
6
7
8
9
library(dplyr)
library(purrr)
df %>%
   mutate(powersum = map2_dbl(n, p, powersum))
#   n p powersum
#1 1 3        1
#2 2 3        9
#3 3 3       36
#4 4 3      100

rowwise 是按行分组的,因此它一次获取每个元素并应用该函数。与 mapply 循环中的概念相同,其中函数应用于每个元素。 powersum 未矢量化,因为序列 1:n 未矢量化。举个例子

1
2
3
n1 <- 1:2
1:n1
[1] 1

Warning message:
In 1:n1 : numerical expression has 2 elements: only the first used

mutate 的情况类似于使用

应用函数

1
with(df, powerset(n, p))

transform

1
transform(df, powersum = powerset(n, p))

两者都对每一列的整行应用函数