sum consecutive values in the same group ordered by date

theofficefan12

I am trying to sum consecutive values that have the same student_id and are ordered by date.

I have tried using sequence(rle()) but the order by dates is not working.

row student_id  date        pass/fail  streak
1   2           2019-05-24  0          0
2   2           2019-05-25  -1         -1
3   1           2019-05-24  1          2
4   1           2019-05-28  -1         -1
5   2           2019-05-23  1          1
6   1           2019-05-27  1          3
7   2           2019-05-28  -1         -2
8   1           2019-05-23  1          1

The streak column is the desired output, and the dates are not in order in the original data frame which I think is what is the problem.

G. Grothendieck

Using the ordering indexes o, order the original data frame by student_id and date. Now, apply cumsum by student_id and pass/fail to pass/fail and finally revert to the original order.

library(data.table)

o <- with(DF, order(student_id, date))

transform(DF[o, ], 
  streak = ave(`pass/fail`, rleid(student_id, `pass/fail`), FUN = cumsum))[order(o), ]

giving:

  ow student_id       date pass.fail streak
1  1          2 2019-05-24         0      0
2  2          2 2019-05-25        -1     -1
3  3          1 2019-05-24         1      2
4  4          1 2019-05-28        -1     -1
5  5          2 2019-05-23         1      1
6  6          1 2019-05-27         1      3
7  7          2 2019-05-28        -1     -2
8  8          1 2019-05-23         1      1

Note

The input in reproducible form:

    Lines <- "ow student_id  date        pass/fail  streak
1   2           2019-05-24  0          0
2   2           2019-05-25  -1         -1
3   1           2019-05-24  1          2
4   1           2019-05-28  -1         -1
5   2           2019-05-23  1          1
6   1           2019-05-27  1          3
7   2           2019-05-28  -1         -2
8   1           2019-05-23  1          1"
DF <- read.table(text = Lines,  header = TRUE, check.names = FALSE)
DF$date <- as.Date(DF$date)
DF$streak <- NULL

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sum of consecutive same values

From Dev

Group by DataFrame based on consecutive ordered values

From Dev

Group and sum values and list down consecutive rows in each group

From Dev

Sum and group consecutive integers

From Dev

R - Sum within group and only if another variable has consecutive values

From Dev

Cumulative sum of first occurence of consecutive True values in a group in Pandas

From Dev

How to sum values under GroupBy and consecutive date conditions?

From Dev

R how Sum values by group by date

From Dev

Group Daily Date Columns by Month and Sum Values

From Dev

How to sum values of two tables and group by date

From Dev

PHP Array Group by same values and SUM

From Dev

How do I sum values by an ID and date, keeping the values ordered by date?

From Dev

Sum consecutive day values

From Dev

Pandas DataFrame group by consecutive same values on multiple columns

From Dev

Find elements with same date and sum values in array

From Dev

group by consecutive values in r

From Dev

Python group dict values on same date

From Dev

Count equal, consecutive values in an ordered rowset

From Dev

Designate groups of consecutive equal values in an ordered dataset

From Dev

Calculate Rank Based on Shared Column Values and Consecutive Date Ranges (same rank for records with consecutive range)

From Dev

First group of values by a ordered query

From Dev

Cumilative sum for only consecutive date

From Dev

Excel sum of values if specific date is in specific period and text values are the same

From Dev

Cumulative sum based on consecutive values

From Dev

Group by consecutive values in one column and select the earliest and latest date for each group

From Dev

Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

From Dev

SUM multiple columns by different condition from same table and then group by date

From Dev

Group by date, sum value and get count of same row in foreach php

From Dev

Group non-unique datetime column by date and sum values in python

Related Related

  1. 1

    Sum of consecutive same values

  2. 2

    Group by DataFrame based on consecutive ordered values

  3. 3

    Group and sum values and list down consecutive rows in each group

  4. 4

    Sum and group consecutive integers

  5. 5

    R - Sum within group and only if another variable has consecutive values

  6. 6

    Cumulative sum of first occurence of consecutive True values in a group in Pandas

  7. 7

    How to sum values under GroupBy and consecutive date conditions?

  8. 8

    R how Sum values by group by date

  9. 9

    Group Daily Date Columns by Month and Sum Values

  10. 10

    How to sum values of two tables and group by date

  11. 11

    PHP Array Group by same values and SUM

  12. 12

    How do I sum values by an ID and date, keeping the values ordered by date?

  13. 13

    Sum consecutive day values

  14. 14

    Pandas DataFrame group by consecutive same values on multiple columns

  15. 15

    Find elements with same date and sum values in array

  16. 16

    group by consecutive values in r

  17. 17

    Python group dict values on same date

  18. 18

    Count equal, consecutive values in an ordered rowset

  19. 19

    Designate groups of consecutive equal values in an ordered dataset

  20. 20

    Calculate Rank Based on Shared Column Values and Consecutive Date Ranges (same rank for records with consecutive range)

  21. 21

    First group of values by a ordered query

  22. 22

    Cumilative sum for only consecutive date

  23. 23

    Excel sum of values if specific date is in specific period and text values are the same

  24. 24

    Cumulative sum based on consecutive values

  25. 25

    Group by consecutive values in one column and select the earliest and latest date for each group

  26. 26

    Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

  27. 27

    SUM multiple columns by different condition from same table and then group by date

  28. 28

    Group by date, sum value and get count of same row in foreach php

  29. 29

    Group non-unique datetime column by date and sum values in python

HotTag

Archive