在 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()
,其中有一个循环:
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)
}
当我运行自定义函数时:
df %>%
mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
test
中的每个变异值都显示相同的值 (13.95)。那没有意义!例如,tibble 中的第一行 (y_val = 2, z_val = 4)
应该是 10.125!
fun_b(x = 1, y = 2, z = 4, times = 1)
这是怎么回事?
尝试以下方法
df %>%
group_by(y_val, z_val) %>%
mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
我得到了 10.125。
您可以按行分组,以便为每一行分别计算函数:
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:
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
我的数据是这样的:
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()
,其中有一个循环:
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)
}
当我运行自定义函数时:
df %>%
mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
test
中的每个变异值都显示相同的值 (13.95)。那没有意义!例如,tibble 中的第一行 (y_val = 2, z_val = 4)
应该是 10.125!
fun_b(x = 1, y = 2, z = 4, times = 1)
这是怎么回事?
尝试以下方法
df %>%
group_by(y_val, z_val) %>%
mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
我得到了 10.125。
您可以按行分组,以便为每一行分别计算函数:
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:
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