如何在R中编写代码来计算R中特定年龄的特定序列

问题描述 投票:0回答:1

我有一个数据集如下:

     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),
      )

我想为 100 万人计算 R 中的后续公式:第一个和最后一个公式与其他组不同。

    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.
     ......

这个程序必须持续到46.5,尽管我只提供一些年龄的信息 对于最后一个时代,

       age47probarea=(f_curve_notch(age46.5+ 
       (f_curve_notch(age46.75*2))/3.

在下一步中:我想计算这个公式,条件是如果特定年龄的 f_cureve_nutched 大于零且不等于相同年龄的 f_curve_tilde,则 f_curve_tilde - f_curve_notched 否则为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)
r function loops data.table mutate
1个回答
1
投票

我认为您正在寻找主导功能:

     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()
© www.soinside.com 2019 - 2024. All rights reserved.