0

New to R: If I have multiple observations for the same ID. How can I tell if event 1 occurred before, after or at the same time as event 2 for each ID?

ID is an integer. The number of observations for each ID varies. Time is just the time period; 1,2,...t. Events 1 and 2 are 0/1 dummies.

I need to be able to count the number of times event 1 occurs before, after or at the same time as event 2. Seems like this should be pretty simple/straight forward, but I haven't had any luck.

UPDATE: this should give a sense of the data:

my.df <-  structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L), Time = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 
5L), Event.1 = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 
1L), Event.2 = c(1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 
0L)), .Names = c("ID", "Time", "Event.1", "Event.2"), class = "data.frame", row.names = c(NA, 
-12L))

Thanks!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
sbha
  • 9,802
  • 2
  • 74
  • 62

1 Answers1

0

Assuming that you want to count how often Event.1 occurred per ID before Event.2 did, this would be a shortcut using ddply

ddply(
  my.df, .(ID), summarize,
  Event.1 = sum( Event.1[ seq_len( which.max( Event.2 ) ) ] )
)

which gives

  ID Event.1
1  1       0
2  2       1
3  3       0
Beasterfield
  • 7,023
  • 2
  • 38
  • 47