Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

counts sequences in R

Tags:

r

count

sequences

id  random  count
a   0        -1
a   1         1
a   1         2
a   0        -1
a   0        -2
a   1         1
a   0        -1
a   1         1
a   0        -1
b   0        -1
b   0        -2
b   1         1
b   0        -1
b   1         1
b   0        -1
b   0        -2
b   0        -3

id is a player , random is binary 0 or 1 , I want to create a count column that counts the sequences of 1's and 0's by player , preferably without loops since the database is very big.

like image 635
Arigold Avatar asked Feb 05 '26 12:02

Arigold


2 Answers

I think this is what you're looking for:

library(data.table)
setDT(DF)[, count := seq_len(.N), by=.(id,rleid(random))]

which gives

    id random count
 1:  a      0     1
 2:  a      1     1
 3:  a      1     2
 4:  a      0     1
 5:  a      0     2
 6:  a      1     1
 7:  a      0     1
 8:  a      1     1
 9:  a      0     1
10:  b      0     2
11:  b      0     3
12:  b      1     1
13:  b      0     1
14:  b      1     1
15:  b      0     1
16:  b      0     2
17:  b      0     3

(In the next version of the data.table package, 1.9.8, there will be a small shortcut setDT(DF)[, count := rowid(rleid(random)), by=id]. I am making this note so I can update the answer later.)


You may also want identifiers for groups of runs:

DF[, rid := rleid(random), by=id]

which gives

    id random count rid
 1:  a      0     1   1
 2:  a      1     1   2
 3:  a      1     2   2
 4:  a      0     1   3
 5:  a      0     2   3
 6:  a      1     1   4
 7:  a      0     1   5
 8:  a      1     1   6
 9:  a      0     1   7
10:  b      0     1   1
11:  b      0     2   1
12:  b      1     1   2
13:  b      0     1   3
14:  b      1     1   4
15:  b      0     1   5
16:  b      0     2   5
17:  b      0     3   5

If you read through the introductory materials on the package, you'll see that these variables can also be created in a single step.

like image 150
Frank Avatar answered Feb 07 '26 03:02

Frank


Here's a dplyr solution

dat %>%
  transform(idx = c(0,cumsum(random[-1L] != random[-length(random)]))) %>%
  group_by(id, idx) %>%
  mutate(count = -1*cumsum(random == 0) + cumsum(random == 1)) %>%
  ungroup() %>%
  select(-idx)

Source: local data frame [17 x 3]

   id random count
1   a      0    -1
2   a      1     1
3   a      1     2
4   a      0    -1
5   a      0    -2
6   a      1     1
7   a      0    -1
8   a      1     1
9   a      0    -1
10  b      0    -1
11  b      0    -2
12  b      1     1
13  b      0    -1
14  b      1     1
15  b      0    -1
16  b      0    -2
17  b      0    -3
like image 30
Whitebeard Avatar answered Feb 07 '26 03:02

Whitebeard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!