Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

data.table - Label a sequence of row elements in a dataframe (R)

I am working with eye-tracking data and I am trying to create a new column 'SaccadePerTrial' which will count (and label) the occurrence of saccades (S) within each unique trial (while ignoring fixations (F)).

This is how my dataframe looks like currently:

Trial | FixationSaccade
1     | F
1     | F
1     | S
1     | S
1     | F
1     | F
1     | S
1     | S

2     | F
2     | F
2     | S
2     | S
2     | F
2     | F
2     | S
2     | S

And this is how the 'SaccadePerTrial' column should look like:

Trial | FixationSaccade | SaccadePerTrial
1     | F               | NA
1     | F               | NA
1     | S               | 1
1     | S               | 1
1     | F               | NA
1     | F               | NA
1     | S               | 2
1     | S               | 2

2     | F               | NA
2     | F               | NA
2     | S               | 1
2     | S               | 1
2     | F               | NA
2     | F               | NA
2     | S               | 2
2     | S               | 2

This is similar to the function rleid(), but I would like the function to ignore values that are not saccades (S). An alternative option (although the less preferred one) would be to rleid() each value in the 'FixationSaccade' column separately (having both the Fs and Ss start from 1).

Does anyone have an idea how I can achieve this? Thank you!

question from:https://stackoverflow.com/questions/65887293/label-a-sequence-of-row-elements-in-a-dataframe-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would do it this way:

dat[, newCol := rleid(FixationSaccade), by = .(Trial)]
dat[FixationSaccade == 'F', newCol := NA]
dat[FixationSaccade == 'S', newCol := rleid(newCol), by = .(Trial)]
# > dat
#     Trial FixationSaccade newCol
#  1:     1               F     NA
#  2:     1               F     NA
#  3:     1               S      1
#  4:     1               S      1
#  5:     1               F     NA
#  6:     1               F     NA
#  7:     1               S      2
#  8:     1               S      2
#  9:     2               F     NA
# 10:     2               F     NA
# 11:     2               S      1
# 12:     2               S      1
# 13:     2               F     NA
# 14:     2               F     NA
# 15:     2               S      2
# 16:     2               S      2

Or with a customized version of rleid:

rleid2 <- function(x){
    r <- rle(x)
    y <- cumsum(r$values == 'S')
    y[r$values == 'F'] <- NA
    r$values <- y
    inverse.rle(r)
}
dat[, newCol2 := rleid2(FixationSaccade), by = .(Trial)]

#     Trial FixationSaccade newCol newCol2
#  1:     1               F     NA      NA
#  2:     1               F     NA      NA
#  3:     1               S      1       1
#  4:     1               S      1       1
#  5:     1               F     NA      NA
#  6:     1               F     NA      NA
#  7:     1               S      2       2
#  8:     1               S      2       2
#  9:     2               F     NA      NA
# 10:     2               F     NA      NA
# 11:     2               S      1       1
# 12:     2               S      1       1
# 13:     2               F     NA      NA
# 14:     2               F     NA      NA
# 15:     2               S      2       2
# 16:     2               S      2       2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...