Every R
problem can be thought of with advantage as the interaction of three objects— an existing object, x , a desired object,y , and a function, f, that will return a value of y given x as an argument. In other words, school algebra— f(x) = y. Any of the objects can be composites.
Here, x is the dataset and what is to be calculated with f is y in my terminology.
\sum\limits_{k=2}^n \frac{C(t_k)+C(t_{k-1})}{2}(t_k-t_{k-1}) produces y, termed x
in the question.
Before translating f into R
, let's first create a toy x.
dat <- data.frame(t = 1:121, C = 300:420)
Given dat
as x we have three pieces of information to work with—t
, C
and their indicOes (positions within their respective vectors).
Our function, f, will have five calculations to make:
-
{C(t_k)+C(t_{k-1})} , which is simply the current row's value of
C
less it's previous row's
-
{C(t_k)+C(t_{k-1})}/2, simply division by 2
-
(t_k-t_{k-1}) , which is simply the current row's value of
t
less than it's previous row's
-
{C(t_k)+C(t_{k-1})}/2 times (t_k-t_{k-1})
- Then, simply add it all up.
How does this look in terms of x?
suppressPackageStartupMessages({
library(dplyr)
})
dat <- data.frame(t = 1:121, C = 300:420)
dat %>% mutate(X = (C + lag(C,1)/2) * (t-lag(t,1))) -> a
sum(a$X,na.rm = TRUE)
#> [1] 64830
find_x <- function(x) {
x %>% mutate(X = (C + lag(C,1)/2) * (t-lag(t,1))) -> a
sum(a$X,na.rm = TRUE)
}
find_x(dat)
#> [1] 64830