Calculating differences in row values in correspondence with separate row values from another column

If I have time-specific data that corresponds to certain events, how can I create a function that calculates the time difference between one event and the next. For example, at 1 minute, "X" event occurs. The same "X" event occurs AGAIN later on in the dataframe at 3 minutes. I would like to run a code that can calculate the difference in time (in this case, 3-1 = 2 minutes) that occurs between "X" events. Then I would like to find the next time difference that occurs after the "X" event that took place at minute 3. I hope this makes sense!

Questions in the abstract are much less likely to attract answers than questions with a reprex.

I agree, if you don't provide a concrete example we can only work with our fantasy.

I guess you need a command line such as diff(dataset$time[dataset$X]).

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Hello -- here is my dataset. I want to find two things specifically: I want to find the number of events that occur after a specific event (for example: how many times does a jump/run/stand event take place after a "run" event). From that, I would like to find the difference in "time" values that occurs in between these events, ultimately generating an average time for each event (for example: after a run event, the average time is "x" until the next stand/jump/run event.) I hope this makes a bit of sense. The day column is so that I can eventually do all of this in a code that will specify the results by the day that it takes place.

dput(jump_edit)
structure(list(Time = c(12, 43, 68, 87, 99, 194, 283, 290, 298, 
302, 385, 409, 586, 599, 689, 750, 801), Event = c("Stand", "Jump", 
"Jump", "Run", "Stand", "Stand", "Stand", "Run", "Run", "Jump", 
"Jump", "Jump", "Stand", "Stand", "Jump", "Stand", "Jump"), Day = c(1, 
1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4)), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -17L), spec = structure(list(
    cols = list(Time = structure(list(), class = c("collector_double", 
    "collector")), Event = structure(list(), class = c("collector_character", 
    "collector")), Day = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))

how does jump relate to event ?
you record a jump when some is standing ? (jump 1 to jump2 happens while standing occurs)

also I don't see a 'day' column ?

1 Like

Apologies -- here is the correct info where you will see the "day" column. Hopefully now it will make more sense. So for example, from time 12 till 43, it is a "stand" event. Then at 43 the next event, a "jump" event, occurs.

dput(jump_edit)
structure(list(Time = c(12, 43, 68, 87, 99, 194, 283, 290, 298,
302, 385, 409, 586, 599, 689, 750, 801), Event = c("Stand", "Jump",
"Jump", "Run", "Stand", "Stand", "Stand", "Run", "Run", "Jump",
"Jump", "Jump", "Stand", "Stand", "Jump", "Stand", "Jump"), Day = c(1,
1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4)), class = c("spec_tbl_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -17L), spec = structure(list(
cols = list(Time = structure(list(), class = c("collector_double",
"collector")), Event = structure(list(), class = c("collector_character",
"collector")), Day = structure(list(), class = c("collector_double",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))

Neglecting the fact that standing is a continuative action while jumping is a punctual action, and the role of different days, you could start with

levels <- unique(dataset$Event)

for(.x in levels) for(.y in levels){
  assign( paste(.x,.y,sep="_then_"), dataset[FALSE,] )
  for(.k in 1:(nrow(dataset)-1))
    if(dataset$Event[.k]==.x&dataset$Event[.k+1]==.y)
      assign( paste(.x,.y,sep="_then_"),
              rbind( get(paste(.x,.y,sep="_then_")), list(Time=diff(dataset$Time)[.k],Event="buh",Day=dataset$Day[.k]) )
      )
}

Does it help?