Note that there are some explanatory texts on larger screens.

plurals
  1. POCounting an event only every X days per subject (in an irregular time series)
    primarykey
    data
    text
    <p>I've got data where I'm counting episodes of care (like ER visits). The trick is, I can't count every single visit, because sometimes a 2nd or 3rd visit is actually a follow-up for a previous problem. So I've been given direction to count visits by using a 30 day "clean period" or "black out period", such that, I look for the first event (VISIT 1) by patient (min date), I count that event, then apply rules so as NOT to count any visits that occur in the 30 days following the first event. After that 30 day window has elapsed, I can begin looking for the 2nd visit (VISIT 2), count that one, then apply the 30 day black out again (NOT counting any visits that occur in the 30 days after visit #2)... wash, rinse, repeat...</p> <p>I have rigged together a very sloppy solution that requires a lot of babysitting and manual checking of steps(see below). I have to believe that there is a better way. HELP! </p> <pre><code>data1 &lt;- structure(list(ID = structure(c(2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L), .Label = c("", "patient1", "patient2", "patient3"), class = "factor"), Date = structure(c(14610, 14610, 14627, 14680, 14652, 14660, 14725, 15085, 15086, 14642, 14669, 14732, 14747, 14749), class = "Date"), test = c(1L, 1L, 1L, 2L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 2L)), .Names = c("ID", "Date", "test"), class = "data.frame", row.names = c(NA, 14L)) library(doBy) ## create a table of first events step1 &lt;- summaryBy(Date~ID, data = data1, FUN=min) step1$Date30 &lt;- step1$Date.min+30 step2 &lt;- merge(data1, step1, by.x="ID", by.y="ID") ## use an ifelse to essentially remove any events that shouldn't be counted step2$event &lt;- ifelse(as.numeric(step2$Date) &gt;= step2$Date.min &amp; as.numeric(step2$Date) &lt;= step2$Date30, 0, 1) ## basically repeat steps above until I dont capture any more events ## there just has to be a better way data3 &lt;- step2[step2$event==1,] data3&lt;- data3[,1:3] step3 &lt;- summaryBy(Date~ID, data = data3, FUN=min) step3$Date30 &lt;- step3$Date.min+30 step4 &lt;- merge(data3, step3, by.x="ID", by.y="ID") step4$event &lt;- ifelse(as.numeric(step4$Date) &gt;= step4$Date.min &amp; as.numeric(step4$Date) &lt;= step4$Date30, 0, 1) data4 &lt;- step4[step4$event==1,] data4&lt;- data4[,1:3] step5 &lt;- summaryBy(Date~ID, data = data4, FUN=min) step5$Date30 &lt;- step5$Date.min+30 ## then I rbind the "keepers" ## in this case steps 1 and 3 above final &lt;- rbind(step1,step3, step5) ## then reformat final &lt;- final[,1:2] final$Date.min &lt;- as.Date(final$Date.min,origin="1970-01-01") ## again, extremely clumsy, but it works... HELP! :) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload