[R] Calculate the difference between dates

Henrique Dallazuanna wwwhsd at gmail.com
Thu Feb 7 13:55:52 CET 2008


If I understand your question, you can try this:

data$M1date <- as.Date(strptime(data$M1date, "%m/%d/%Y"))
data$M2date <- as.Date(strptime(data$M2date, "%m/%d/%Y"))
data$Days <- data$M2date - data$M1date



On 07/02/2008, Tom Cohen <tom.cohen78 at yahoo.se> wrote:
> Dear list,
>
> I have two data columns (part of big data frame) containing dates presenting the dates when
> two measurements (M1 and M2) were taken. The data consists of 73 individuals divided
> in different groups. Each group was examined at different time points (see M1 date),but
> the measurements (M1 and M2) within each group should be taken in the same day but due
> to some practical issues, some M2-measurents were taken on another day. All individuals
> measured at the same day in column M1date belong to one group.
> For example, 24 measurements were taken 8/15/2005 (M1date column,one group of individual)
> but only 4 M2-mesurements were taken at the same day, the 24-2=20 M2-measurements were
> taken 2-65 days later within this group.
>   For analysis, I need to know how many days the M2-measurements were taken if
> they were not taken at the same day as M1-measurements within each group. Each of the
> different dates in column M1date would reflect the starting date for each group,for example
> 8/15/2005 is starting date for one group, and 8/8/2005 is a starting date for another group, so if
> M2-measurements wíthin group were taken for example 8/15/2005, then we say that
> they were taken at day 1, and if they were taken 8/16/2005 we say they were taken
> 2 days after the starting date for that group and so on. For group with starting date
> 8/8/2005, if the M2-measurements were taken at the same date, then these would be considered
> taken at day 1  and for those that were taken 8/9/2005, we say they were taken at day 2 for this group
> In the below data, I have manually calculated  the number of days when M2-measurements
> were taken if they were not taken at
> the starting day for some individuals (column Days). Is there any automatic ways to do this in R ?
>
>       M1date     M2date  Days
> 75 8/15/2005  8/15/2005  1
> 79 8/15/2005  8/16/2005  2
> 76 8/15/2005  8/15/2005  1
> 84 8/15/2005  8/16/2005  2
> 74 8/15/2005  8/15/2005  1
> 80 8/15/2005  8/16/2005  2
> 77 8/15/2005  8/15/2005  1
> 81 8/15/2005  8/16/2005  2
> 82 8/15/2005  8/16/2005  2
> 78 8/15/2005  8/16/2005  2
> 83 8/15/2005  8/16/2005  2
> 85 8/15/2005 10/17/2005  62
> 2  8/15/2005 10/19/2005  64
> 1  8/15/2005 10/18/2005  63
> 5  8/16/2005 10/19/2005  65
> 3  8/15/2005 10/19/2005  65
> 4  8/15/2005 10/19/2005  65
> 6  8/15/2005 10/19/2005  65
> 12  8/8/2005   8/9/2005   2
> 10  8/8/2005   8/9/2005   2
> 11  8/8/2005   8/9/2005   2
> 8   8/8/2005  11/7/2005
> 7   8/8/2005  11/8/2005
> 9   8/8/2005   8/8/2005
> 29  8/8/2005 11/10/2005
> 25  8/8/2005  11/9/2005
> 30  8/8/2005 11/10/2005
> 28  8/8/2005  11/9/2005
> 32 8/15/2005 11/10/2005
> 33 8/15/2005 11/10/2005
> 31 8/15/2005 11/10/2005
> 24 8/15/2005  11/9/2005
> 26 8/15/2005  11/9/2005
> 27 8/15/2005  11/9/2005
> 14 7/31/2006 11/15/2006
> 18 7/31/2006 11/13/2006
> 13 7/31/2006 11/15/2006
> 16 7/31/2006 11/16/2006
> 20 7/31/2006 11/14/2006
> 17 7/31/2006 11/16/2006
> 19 7/31/2006 11/13/2006
> 37  8/7/2006   8/7/2006
> 39  8/7/2006   8/7/2006
> 42  8/7/2006  9/20/2006
> 49  8/7/2006  9/21/2006
> 52  8/7/2006  9/21/2006
> 50  8/7/2006  9/21/2006
> 47  8/7/2006  9/21/2006
> 38  8/7/2006   8/7/2006
> 45  8/7/2006  9/19/2006
> 43  8/7/2006  9/20/2006
> 48  8/7/2006  9/21/2006
> 36  8/7/2006   8/7/2006
> 44  8/7/2006  9/20/2006
> 46  8/7/2006  9/19/2006
> 51  8/7/2006  9/21/2006
> 41  8/7/2006   8/8/2006
> 40  8/7/2006   8/8/2006
> 68 7/31/2006  9/28/2006
> 59 7/31/2006   8/1/2006
> 69 7/31/2006  9/28/2006
> 71 7/31/2006  9/28/2006
> 58  8/1/2006  9/27/2006
> 60 7/31/2006   8/1/2006
> 70 7/31/2006  9/28/2006
> 66 7/31/2006  9/28/2006
> 67 7/31/2006  9/28/2006
> 72 7/31/2006  9/28/2006
> 64  8/1/2006   8/1/2006
> 61 7/31/2006   8/1/2006
> 62 7/31/2006   8/1/2006
> 63 7/31/2006   8/1/2006
> 65 7/31/2006   8/1/2006
>
>   Thanks for any help,
> Tom
>
>
>
> ---------------------------------
> Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling.
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list