I have a dataSet as follows:
library("tidyverse")
DataSet2<-
tibble(
id = c(1,2,3,4,5,6,7,8,9,10),
AGE= c(15,15.25,15.5, 15.75, 16, 16.25, 16.5, 16.75, 46.5, 46.75),
f_curve_notch= c(25.14,30.28,43.33,43.33,25.14,25.14,25.14,25.14,67
,33.77),
f_curve_tilde=
c(.2514,30.28,43.33,43.33,25.14,25.14,25.14,25.14,.67
,33.77),
)
I want to calculate the ensuing formula in R for 1 million individuals: The first and last formula differs from other groups.
age15probarea=((f_curve_notch(age15)*2)+
(f_curve_notch(age15.25)*2)+f_curve_notch(age15.5)/5.
age16probarea=(f_curve_notched(age15.5)+
(f_curve_notch(age15.75)*2)+
(f_curve_notch(age16)*2)+
(f_curve_notch(age16.25)*2)+f_curve_notch(age16.5)/8.
......
This procedure has to continue to 46.5 although I provide information just for some age and for the last age,
age47probarea=(f_curve_notch(age46.5+
(f_curve_notch(age46.75*2))/3.
In the next step: I want to calculate this formula with a condition in which if f_cureve_nutched of a specific age is greater than zero and not equal to f_curve_tilde of the same age then f_curve_tilde - f_curve_notched else be the previous amount for age16probarea:
DataSet$age16probarea<- ifelse ((f_curve_notch(age15.5)>0
& f_curve_notch(age15.75) != f_curve_tilde(age15.75),
age16probarea+ ((f_curve_tilde(age15.75) -
f_curve_notch(age15.75))*0.125), age16probarea)
I think you are looking for lead function:
DataSet2<-DataSet2 %>%
mutate(prob = (f_curve_notch + 2 * lead(f_curve_notch) + 2 *
lead(f_curve_notch, 2) + 2 * lead(f_curve_notch, 3) + lead(f_curve_notch,
4))/8) %>%
as.data.frame()