My data looks like this:
library(tidyverse)
df <- tribble(
~y_val, ~z_val,
2, 4,
5, 3,
8, 2,
1, 1,
9, 3)
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))
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)
What's going on here?
df %>% rowwise() %>% mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))
or just take out thesum
calls so your functions are vectorizable – alistaire