Domanda

How can I calculate if the ID appear consecutively for less then 5 days ? also calculate the day difference between same ID record . I really cannot get the logic for this problem and I did not know what I can start with.

(The sample data given below is just a sample , my actual data is in huge volume.Hence,optimization is needed.)

sample data :

 sample<- data.frame(
  id=c("A","B","C","D","A","C","D","A","C","D","A","D","A","C"),
  date=c("1/3/2013","1/3/2013", "1/3/2013","1/3/2013","2/3/2013","2/3/2013",
    "2/3/2013","3/3/2013","3/3/2013",
     "3/3/2013",
     "4/3/2013",
     "4/3/2013",
     "5/3/2013",
    "5/3/2013"
      )
      )

Expected Output:

output<- data.frame(
id=c("A","A","A","A","A","B","C","C","C","C","D","D","D","D","D","D","D"),
date=c("1/3/2013",
     "2/3/2013",
     "3/3/2013",
     "4/3/2013",
     "5/3/2013",
     "1/3/2013",
     "1/3/2013",
     "2/3/2013",
     "3/3/2013",
     "5/3/2013",
     "1/3/2013",
     "2/3/2013",
     "3/3/2013",
     "4/3/2013",
     "5/3/2013",
     "6/3/2013",
     "7/3/2013" ),
 num=c(0,1,2,3,4,0,0,1,2,4,0,1,2,3,4,5,6)
)

Calculation Logic :

Do calculation on the date difference. For example, 1/3 to 2/3 is 1 day difference so the row of 2/3, column idu:1 . 2/3 to 3/3 is 1 day difference so add on 1 row 3/3 , column idu:2 . 3/3 to 5/3 is 2 day difference so add 2 to idu . row 5/3 , column idu : 4 . (Base on same ID)

Date | idu 
1/3  |  0
2/3  |  1
3/3  |  2
5/3  |  4

Thanks in advance.

È stato utile?

Soluzione

sample<- data.frame(
  id=c("A","B","C","D","A","C","D","A","C","D","A","D","A","C"),
  date=c("1/3/2013","1/3/2013", "1/3/2013","1/3/2013","2/3/2013","2/3/2013",
         "2/3/2013","3/3/2013","3/3/2013",
         "3/3/2013",
         "4/3/2013",
         "4/3/2013",
         "5/3/2013",
         "5/3/2013"), stringsAsFactors = F)

library(lubridate)
sample$date <- dmy(sample$date)
sample1 <- sample[order(sample$id, sample$date), ]
sample1$idu <- unlist(sapply(rle(sample1$id)$lengths, seq_len)) -1

   id       date idu
1   A 2013-03-01   0
5   A 2013-03-02   1
8   A 2013-03-03   2
11  A 2013-03-04   3
13  A 2013-03-05   4
2   B 2013-03-01   0
3   C 2013-03-01   0
6   C 2013-03-02   1
9   C 2013-03-03   2
14  C 2013-03-05   3
4   D 2013-03-01   0
7   D 2013-03-02   1
10  D 2013-03-03   2
12  D 2013-03-04   3

In order to add a time lag column, several options are available. I'd simply do

sample1$diff <- c(0, int_diff(sample1$date)/days(1))
# Remainder cannot be expressed as fraction of a period.
#   Performing %/%.

> sample1
   id       date idu diff
1   A 2013-03-01   0    0
5   A 2013-03-02   1    1
8   A 2013-03-03   2    1
11  A 2013-03-04   3    1
13  A 2013-03-05   4    1
2   B 2013-03-01   0   -4
3   C 2013-03-01   0    0
6   C 2013-03-02   1    1
9   C 2013-03-03   2    1
14  C 2013-03-05   3    2
4   D 2013-03-01   0   -4
7   D 2013-03-02   1    1
10  D 2013-03-03   2    1
12  D 2013-03-04   3    1

And do further changes as needed. replacing all negative values with 0.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top