R vectorization and dplyr mutate bug?
我有一个简单的函数来计算矢量化和(在这个例子中我使用的是幂和,但我实际使用的是一个更复杂的函数)和一个
如果您能深入了解正在发生的事情,我将不胜感激。
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? |
如果我们需要
中对应的选项
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 |
1 2 3 | n1 <- 1:2 1:n1 [1] 1 |
Warning message:
In 1:n1 : numerical expression has 2 elements: only the first used
应用函数
1 | with(df, powerset(n, p)) |
或
1 | transform(df, powersum = powerset(n, p)) |
两者都对每一列的整行应用函数