R-当前列减去下一x列的总和,直到满足条件

时间:2017-12-06 10:39:02

标签: r

我尝试用R完成sum列,但它不起作用。 它实际上很简单: sum =当前行的点数 - 总和(奖励行直到第一次参与等于0)如果可能的话,我更喜欢清晰简短的代码。

Pupil   participation   bonus   points
  2           55          6       10
  2           33          3       10
  2           88          9       10
  2           44          8       10
  2           44          0       10
  2           66          7       10
  2            0          0       10
  1           22          11      10
  1           33          12      10
  1           33          13      10
  1            3          13      10
  1           44          1       10
  1            0          3       10
  1            0          0       10

Pupil   participation   bonus   points   sum
  2           55          6       10      10-(6+3+9+8+0+7)
  2           33          3       10      10-(3+9+8+0+7)
  2           88          9       10      10-(9+8+0+7)
  2           44          8       10      10-(8+0+7)
  2           44          0       10      10-(0+7)
  2           66          7       10      10-(7)
  2            0          0       0        0
  1           22          11      10      10-(11+12+13+13+1)
  1           33          12      10      10-(12+13+13+1)
  1           33          13      10      10-(13+13+1)
  1            3          13      10      10-(13+1
  1           44          1       10      10-(1)
  1            0          3       10       0
  1            0          0       0        0

由于

3 个答案:

答案 0 :(得分:0)

我们可以使用tidyverse

library(dplyr)
library(purrr)
df1 %>% 
    group_by(Pupil) %>% 
    mutate(points = replace(points, tail(which(participation == 0), 1), 0),
     bonus = bonus *(bonus != 0 & points != 0),
     Sum = (points - (accumulate_right(bonus*(participation != 0), 
             `+`)))* (participation !=0))
# A tibble: 14 x 5
# Groups:   Pupil [2]
#   Pupil participation bonus points   Sum
#   <int>         <int> <int>  <dbl> <dbl>
# 1     2            55     6     10   -23
# 2     2            33     3     10   -17
# 3     2            88     9     10   -14
# 4     2            44     8     10    -5
# 5     2            44     0     10     3
# 6     2            66     7     10     3
# 7     2             0     0      0     0
# 8     1            22    11     10   -40
# 9     1            33    12     10   -29
#10     1            33    13     10   -17
#11     1             3    13     10    -4
#12     1            44     1     10     9
#13     1             0     3     10     0
#14     1             0     0      0     0

答案 1 :(得分:0)

这是一个基本的R split-apply-combine方法:

private void startLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}

private void stopLocationUpdate() {
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        return;
    }
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
}

答案 2 :(得分:0)

另一种解决方案。

df  =read.table(text="Pupil   participation   bonus   points
  2           55          6       10
  2           33          3       10
  2           88          9       10
  2           44          8       10
  2           44          0       10
  2           66          7       10
  2            0          0       10
  1           22          11      10
  1           33          12      10
  1           33          13      10
  1            3          13      10
  1           44          1       10
  1            0          3       10
  1            0          0       10",header=T)

library(data.table)
df = rbindlist(lapply(split(df,with(rle(df$participation==0),
                 rep(seq(length(values)),lengths))), 
                 function(x) {x$sum2=x$points-rev(cumsum(rev(x$bonus))); x}))
df$sum2[df$participation==0]=0

输出:

    Pupil participation bonus points                sum sum2
 1:     2            55     6     10   10-(6+3+9+8+0+7)  -23
 2:     2            33     3     10     10-(3+9+8+0+7)  -17
 3:     2            88     9     10       10-(9+8+0+7)  -14
 4:     2            44     8     10         10-(8+0+7)   -5
 5:     2            44     0     10           10-(0+7)    3
 6:     2            66     7     10             10-(7)    3
 7:     2             0     0      0                  0    0
 8:     1            22    11     10 10-(11+12+13+13+1)  -40
 9:     1            33    12     10    10-(12+13+13+1)  -29
10:     1            33    13     10       10-(13+13+1)  -17
11:     1             3    13     10           10-(13+1   -4
12:     1            44     1     10             10-(1)    9
13:     1             0     3     10                  0    0
14:     1             0     0      0                  0    0
相关问题