Skip to content

Count number of change in a specified time interval

3 messages · jim holtman, Bert Gunter

#
Try this, but I only get 2 changes for CB27A instead of you indicated 3:
+ CB26A    201302         1
+ CB26A    201302         0
+ CB26A    201302         0
+ CB26A    201303         1
+ CB26A    201303         1
+ CB26A    201304         0
+ CB26A    201305         1
+ CB26A    201305         0
+ CB26A    201306         1
+ CB27A    201304         0
+ CB27A    201304         0
+ CB27A    201305         1
+ CB27A    201306         1
+ CB27A    201306         0
+ CB27A    201307         0
+ CB27A    201308         1", header = TRUE, as.is = TRUE)
+     , {
+         # determine the end date as 3 months from the first date
+         endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
+         # extract what is changing
+         changes <- ATT_1[(MYD >= MYD[1L]) & (MYD <= endDate)]
+         # now count the changes
+         list(nChanges = sum(head(changes, -1L) != tail(changes, -1L)))
+       }
+     , by = CASE_ID
+     ]
   CASE_ID nChanges
1:   CB26A        5
2:   CB27A        2

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Wed, Jul 30, 2014 at 3:08 AM, Abhinaba Roy <abhinabaroy09 at gmail.com> wrote:
#
Or ?rle

Bert



Sent from my iPhone -- please excuse typos.
#
Here is the solution using 'rle':
+  CB26A    201302         1
+  CB26A    201302         0
+  CB26A    201302         0
+  CB26A    201303         1
+  CB26A    201303         1
+  CB26A    201304         0
+  CB26A    201305         1
+  CB26A    201305         0
+  CB26A    201306         1
+  CB27A    201304         0
+  CB27A    201304         0
+  CB27A    201305         1
+  CB27A    201306         1
+  CB27A    201306         0
+  CB27A    201307         0
+  CB27A    201308         1", header = TRUE, as.is = TRUE)
+      , {
+          # determine the end date as 3 months from the first date
+          endDate <- seq(MYD[1L], by = '3 months', length = 2)[2L]
+          # now count the changes
+          list(nChanges = length(rle(ATT_1[(MYD >= MYD[1L]) & (MYD <=
endDate)])[[1L]]) - 1L)
+        }
+      , by = CASE_ID
+      ]
   CASE_ID nChanges
1:   CB26A        5
2:   CB27A        2

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Mon, Aug 4, 2014 at 11:39 AM, Bert Gunter <gunter.berton at gene.com> wrote: