Creating a column whose value at index `i` depends on the values of another column around index `i`

49 Views Asked by At

Consider this R data frame:

# A tibble: 795 × 8
   Epoch `Ultra-slow` alpha   beta   delta   gamma  theta Stage
   <fct>        <dbl> <dbl>  <dbl>   <dbl>   <dbl>  <dbl> <chr>
 1 8           384.   74.1  13.6   1926.   0.316   204.   A    
 2 9            21.9  10.7   1.79   155.   0.0518   23.4  A    
 3 10            9.22  4.28  0.360   19.1  0.0138    3.22 A    
 4 11            2.89  5.02  0.792   31.5  0.0156    4.42 A    
 5 12           20.5   2.79  0.381   50.9  0.0159    4.15 A    
 6 13            5.49  6.70  0.395   19.7  0.0114    3.64 A    
 7 14            6.27  5.33  0.241    5.39 0.00406   4.12 A    
 8 15            5.23  4.70  0.275    7.25 0.00230   4.15 A    
 9 22            3.25  1.03  0.170   21.8  0.00172   5.01 2    
10 23            3.57  1.93  0.240   14.7  0.00191   4.44 2    
# ℹ 785 more rows

The Stage column has values 1, 2, 3, 4, R, M, A. I want to add a new column, NR_P, satisfying this:

  • The column is TRUE if the row is within a NREM period, FALSE otherwise.

A NREM period is defined as a succession of stages 2, 3 or 4 of ≥ 30 Epochs duration terminated by stage R or a period of wakefulness A of ≥ 10 Epochs.

As you can see, the value of this column depends on a ranging set of values of the column Stage and how that set of values was terminated. I was not able to implement this. How can one create such a column?

0

There are 0 best solutions below