在组内创建相对于基线的滞后差异变量

时间:2019-06-12 16:46:16

标签: r dplyr

我想要一个变量,该变量与组内基线之间存在滞后差异。我拥有平衡的面板数据。

my_data <- data.frame(id = c(1,1,1,2,2,2,3,3,3), group = c(1,2,3,1,2,3,1,2,3), score=as.numeric(c(0,150,170,80,100,110,75,100,0)))

  id group score
1  1     1     0
2  1     2   150
3  1     3   170
4  2     1    80
5  2     2   100
6  2     3   110
7  3     1    75
8  3     2   100
9  3     3     0

我希望它看起来像这样:

  id group score lag_diff_baseline
1  1     1     0                NA
2  1     2   150               150
3  1     3   170               170
4  2     1    80                NA
5  2     2   100                20
6  2     3   110                30
7  3     1    75                NA
8  3     2   100                25
9  3     3     0               -75

2 个答案:

答案 0 :(得分:0)

我错过了简单的答案,在这里发布是因为我在SO上找不到它。

$needles = [
    '/route-1/' => [
        'gravityforms/gravityforms.php',
    ],
    '/shop/' => [
        'gravityforms/gravityforms.php',
        'ewww-image-optimizer/ewww-image-optimizer.php',
        'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
    ],
    'search' [
        'ewww-image-optimizer/ewww-image-optimizer.php',
        'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php',
    ],
];



$haystack = 'x' . $_SERVER['REQUEST_URI']; // prepend something to string 

foreach ($needles as $needle => $targets) {

    if (strpos($haystack, $needle) !== false) {

         foreach( $targets as $target ) {
               array_splice($plugins, array_search($target, $plugins), 1);
         }

    }

}

答案 1 :(得分:0)

@Liam答案的data.table版本

library(data.table)
setDT(my_data)
my_data[,.(id,group,score,lag_diff_baseline = score-first(score)),by = id]