How to make epochs in R using filter

Viewed 44

I have a large dataframe in which I want to subset data starting from a value in another column, thereby adding a certain amount of time. To make it more clear, the dataframe looks something like this:

df <- data.frame(TimeStamp = c(100, 150, 200, 250, 300, 350, 400, 450, 500, 550), 
             Marker = c("start_trial", "", "", "", "", "start_trial", "", "", "", ""), 
             size=c(3, 5, 1, 5, 4, 6 , 9, 2, 4, 8),
             trial=c("trial 1", "trial 1", "trial 1", "trial 1", "trial 1", "trial 2", "trial 2", "trial 2", "trial 2", "trial 2"))

   TimeStamp      Marker size   trial
1        100 start_trial    3 trial 1
2        150                5 trial 1
3        200                1 trial 1
4        250                5 trial 1
5        300                4 trial 1
6        350 start_trial    6 trial 2
7        400                9 trial 2
8        450                2 trial 2
9        500                4 trial 2
10       550                8 trial 2

What I want to do is keep the rows that are between 100 and 150ms starting from the TimeStamp value associated with each start trial value, thereby making epochs. So, in this example I would like my data to end up like this:

df2 <- data.frame(TimeStamp = c(200, 250, 450, 500), Marker = c("", "", "", ""), size=c(1, 5, 2, 4), trial=c("trial 1", "trial 1", "trial 2", "trial 2"))

  TimeStamp Marker size   trial
1       200           1 trial 1
2       250           5 trial 1
3       450           2 trial 2
4       500           4 trial 2

My actual data has almost a million rows, with thousands of these "start trials", and the TimeStamp values are not neatly divided in timebins of 50ms.

I hope someone can help me out. Thanks in advance!

1 Answers

Create a group by doing the cumulative sum of logical expression ('grp'), and filter those rows where the difference in 'Timestamp' from the first element is between 100 and 150

library(dplyr)
df %>%
   group_by(grp = cumsum(Marker == 'start_trial')) %>% 
   filter(between(TimeStamp - first(TimeStamp), 100, 150) ) %>%
   ungroup %>%
   select(-grp)

-output

# A tibble: 4 x 4
#  TimeStamp Marker  size trial  
#      <dbl> <chr>  <dbl> <chr>  
#1       200 ""         1 trial 1
#2       250 ""         5 trial 1
#3       450 ""         2 trial 2
#4       500 ""         4 trial 2
Related