I have a data frame with as follows
lower <- c(1,5,15)
upper <-c(5,15,30)
df<-data.frame(lower,upper)
I would like to use dplyr's mutate to create a new variable of the area under the curve of a defined function. The function is as follows.
my_fun <- function(x){y = 1.205016 + 0.03796243 * log(x)}
I am using the integral()
function from the pracma
package to find the area under the curve. When I use this function on an a pair of upper and lower values it runs with no Errors as follows.
integral(my_fun, 1,5)
[1] 4.973705`
However, when I try to run this same function using dplyr's mutate, I get the following.
new_df <- df %>%
mutate(new_variable = integral(my_fun, lower, upper))
Error in integral(my_fun, lower, upper) : length(xmin) == 1 is not TRUE
It seems that the integral
function must be reading the whole vectors df$lower
and df$upper
rather than the individual pairs of values 1,5
. Is there a solution to this using dplyr's mutate
, or should I be looking for other solutions.
I did some looking around and the only instances of this error related to mutate did not seem to address the issue I have here.