Is it possible to calculate integrals from data frame in R?
Example.
t <- data.frame(x1 = c(200),
x2 = c(2),
x3 = c(1:500))
If t is table, is it possible to generate new column with integral results like with other operations?
I tried with this code, but it didn't work.
Func <- function(z) {(t$x1+t$x2+t$x3)*z}
t$new <- integrate(Func, lower = 1, upper = 2)
With dplyr
:
library(dplyr)
Func <- function(z,t) {t*z}
t %>% rowwise %>%
mutate(new = integrate(function(z) Func(z,x1+x2+x3), lower = 1, upper = 2)$value )
# A tibble: 500 x 4
# Rowwise:
x1 x2 x3 new
<dbl> <dbl> <int> <dbl>
1 200 2 1 305.
2 200 2 2 306.
3 200 2 3 308.
4 200 2 4 309
5 200 2 5 310.
6 200 2 6 312
7 200 2 7 314.
8 200 2 8 315
9 200 2 9 316.
10 200 2 10 318
# ... with 490 more rows