组内的滞后函数

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

我想编写代码以在每组内进行计算,滞后差的总和如下表所示:

ID  x rank  U   R   Required Output Value
1   1   1   U1  R1  -
1   1   2   U2  R2  R2-U1
1   1   3   U3  R3  (R3-U2) + (R3-U1)
1   1   4   U4  R4  (R4-U3) + (R4-U2) + (R4-U1)
1   0   5   U5  R5  R5
1   0   6   U6  R6  R6
1   0   7   U7  R7  R7
2   1   1   U8  R8  -
2   1   2   U9  R9  R9-U8
2   1   3   U10 R10 (R10-U9) + (R10 - U8)
2   1   4   U11 R11 (R11-U10) + (R11 - U9) + (R11 - U8)
3   1   1   U12 R12 -
3   0   2   U13 R13 R13
3   0   3   U14 R14 R14

ID是唯一的组标识符。 x是布尔值,根据其值,所需的输出为与先前值之差或相同周期值的总和。 “等级”是等级排序列,最大等级可以在每个组中变化。 “ U”和“ R”是关注的主要列。

要给出一个数值示例,我需要以下内容:

ID  x rank  U   R   Required Output Value
1   1   1   10  7   -
1   1   2   9   11  1
1   1   3   10  10  1 + 0 = 1
1   1   4   11  13  3+4+3 = 10
1   0   5   7   8   8
1   0   6   8   8   8
1   0   7   5   7   7
2   1   1   3   2   -
2   1   2   9   15  12
2   1   3   13  14  16
2   1   4   1   14  17
3   1   1   12  1   -
3   0   2   14  9   9
3   0   3   1   11  11

用于生成此表的R代码:

ID = c(rep(1,7),rep(2,4),rep(3,3))
x = c(rep(1,4),rep(0,3),rep(1,5),rep(0,2))
rank = c(1:7,1:4,1:3)
U = c(10,9,10,11,7,8,5,3,9,13,1,12,14,1)
R = c(7,11,10,13,8,8,7,2,15,14,14,1,9,11)
dat = cbind(ID,x,rank,U,R)
colnames(dat)=c("ID","x","rank","U","R")
r grouping lag
2个回答
1
投票

这里是tidyverse解决方案:

library(dplyr)
library(tidyr)

dat %>%
  as_tibble() %>%
  group_by(ID) %>%
  mutate(`Required Output Value` = replace_na(ifelse(x, lag(rank) * R - lag(cumsum(U)), R), "-"))

结果:

# A tibble: 14 x 6
# Groups:   ID [3]
      ID     x  rank     U     R `Required Output Value`
   <dbl> <dbl> <dbl> <dbl> <dbl> <chr>                  
 1     1     1     1    10     7 -                      
 2     1     1     2     9    11 1                      
 3     1     1     3    10    10 1                      
 4     1     1     4    11    13 10                     
 5     1     0     5     7     8 8                      
 6     1     0     6     8     8 8                      
 7     1     0     7     5     7 7                      
 8     2     1     1     3     2 -                      
 9     2     1     2     9    15 12                     
10     2     1     3    13    14 16                     
11     2     1     4     1    14 17                     
12     3     1     1    12     1 -                      
13     3     0     2    14     9 9                      
14     3     0     3     1    11 11 

0
投票

这里是使用ave的R基础解决方案>

dat <- within(dat,output <- ave(R,ID,x, FUN = function(v) v*(seq(v)-1))-ave(U,ID,x, FUN = function(v) c(NA,cumsum(v)[-length(v)])))
dat <- within(dat, output <- ifelse(x==0,R,output))

诸如此类

> dat
   ID x rank  U  R output
1   1 1    1 10  7     NA
2   1 1    2  9 11      1
3   1 1    3 10 10      1
4   1 1    4 11 13     10
5   1 0    5  7  8      8
6   1 0    6  8  8      8
7   1 0    7  5  7      7
8   2 1    1  3  2     NA
9   2 1    2  9 15     12
10  2 1    3 13 14     16
11  2 1    4  1 14     17
12  3 1    1 12  1     NA
13  3 0    2 14  9      9
14  3 0    3  1 11     11
© www.soinside.com 2019 - 2024. All rights reserved.