0
votes

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?

2
You just need to group rowwise, i.e. df %>% rowwise() %>% mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1)) or just take out the sum calls so your functions are vectorizablealistaire

2 Answers

1
votes

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

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
1
votes

Try the following

df %>% 
    group_by(y_val, z_val) %>% 
    mutate(test = fun_b(x = 1, y = y_val, z = z_val, times = 1))

It got me 10.125.