Skip to content
Prev 306407 / 398506 Next

Calculating number of elapsed days from starting date

On 27/09/12 17:16, Marcel Curlin wrote:
X <- data
X$date <- with(X,as.Date(date,format="%m/%d/%y"))
X$Days <- unlist(with(X,tapply(date,Person,function(x){x-x[1]})))

And then if you *really* want to keep that shaganappi date format:

X$date <- data$date
X
# Giving:
Person    date Days
1    bob  1/1/00    0
2    bob  1/2/00    1
3    bob  1/3/00    2
4   dave  1/7/00    0
5   dave  1/8/00    1
6   dave 1/10/00    3
7  kevin  1/2/00    0
8  kevin  1/3/00    1
9  kevin  1/4/00    2

BTW:  You really *shouldn't* call your data "data".  See:

     fortune("dog")

         cheers,

             Rolf Turner