在dplyr :: mutate中应用自定义函数时出现意外值 [英] Unexpected values while applying custom function in dplyr::mutate
问题描述
我的数据如下:
library(tidyverse)
df <- tribble(
~y_val, ~z_val,
2, 4,
5, 3,
8, 2,
1, 1,
9, 3)
我有自定义函数 fun_b()
,我想通过dplyr :: mutate调用将其应用于数据框.但是, fun_b()
使用函数 fun_a()
,该函数内部具有循环:
I have custom function fun_b()
that I would like to apply to the data frame with a dplyr::mutate call. However, fun_b()
uses function fun_a()
which has a loop inside of it:
fun_a <- function(x, y, z, times = 1) {
df <- data.frame()
for (i in 1:times) {
x <- x * 2 + i * x
y <- y / 3 + i * y
z <- z + 1 + z * i
d <- data.frame(x, y, z)
df <- rbind(df, d)
}
return(df)
}
fun_b <- function(x, y, z, times = 1) {
df <- fun_a(x, y, z, times)
x_r <- sum(df$x)
y_r <- sum(df$y)
z_r <- sum(df$z)
val <- x_r / y_r * z_r
return(val)
}
当我运行自定义功能时:
When I run the custom function:
df %>%
mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
test
中的每个突变值都显示相同的值(13.95).那没有道理!例如,标题(y_val = 2,z_val = 4)
中的第一行应为10.125!
Every mutated value in test
shows the same value (13.95). That doesn't make sense! For instance, the first row in the tibble (y_val = 2, z_val = 4)
should be 10.125!
fun_b(x = 1, y = 2, z = 4, times = 1)
这是怎么回事?
推荐答案
您可以按行分组,以便对每一行分别求值该函数:
You can group rowwise so the function gets evaluated separately for each row:
df %>%
rowwise() %>%
mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
## Source: local data frame [5 x 3]
## Groups: <by row>
##
## # A tibble: 5 × 3
## y_val z_val test
## <dbl> <dbl> <dbl>
## 1 2 4 10.12500
## 2 5 3 3.15000
## 3 8 2 1.40625
## 4 1 1 6.75000
## 5 9 3 1.75000
或编辑 fun_b
进行矢量化,或直接按R:
or edit fun_b
to get so it's vectorized, or just let R:
df %>% mutate(test = Vectorize(fun_b)(x = 1, y = y_val, z = z_val, times = 1))
## # A tibble: 5 × 3
## y_val z_val test
## <dbl> <dbl> <dbl>
## 1 2 4 10.12500
## 2 5 3 3.15000
## 3 8 2 1.40625
## 4 1 1 6.75000
## 5 9 3 1.75000
这篇关于在dplyr :: mutate中应用自定义函数时出现意外值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!