# Summation of a function with a given formula

This is the formula for the function. The dataset contains the variable k (row no.), t (0-120) and C.
I will like to apply a summation using N = 121.

My current written code is as follows: AMT <- function(tk,tk_0,conc,conc_0){x <- 1/2*sum((conc+conc_0)*(tk-tk_0))}

I am not so sure how to proceed forward with the code as I am supposed to write a function that takes time and concentration as arguments to calculate AMT.

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:

1. {C(t_k)+C(t_{k-1})} , which is simply the current row's value of C less it's previous row's
2. {C(t_k)+C(t_{k-1})}/2, simply division by 2
3. (t_k-t_{k-1}) , which is simply the current row's value of t less than it's previous row's
4. {C(t_k)+C(t_{k-1})}/2 times (t_k-t_{k-1})
5. 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


Hi technocrat,

Just to clarify what is X and a in the following code above?

Regards.

They are temporary objects—X creates a variable from dat that contains C(tk)+C(tk−1)/2 \ times\ (tk−tk−1) for each t, and a is the resultant data.frame.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.