I have data in the following format: > DATE [1] "01/13/2004" In order to find the difference between two data points, I presently use brute force to calculate the day of the year: > strptime(DATE, format="%m/%d/%Y")$yday [1] 12 Although this works, it may not be robust over different years. I assume that R is sufficiently clever that a much simpler approach exists, e.g., can I calculate the number of days since some fixed time? Unfortunately, the man pages and FAQ did not lead me to an obvious solution. Any help appreciated. Dennis Dennis Fisher MD P < (The "P Less Than" Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-415-564-2220 www.PLessThan.com
Dennis Fisher <fisher at plessthan.com> writes:> I have data in the following format: > > > DATE > [1] "01/13/2004" > > In order to find the difference between two data points, I presently > use brute force to calculate the day of the year: > > > strptime(DATE, format="%m/%d/%Y")$yday > [1] 12 > > Although this works, it may not be robust over different years. I > assume that R is sufficiently clever that a much simpler approach > exists, e.g., can I calculate the number of days since some fixed > time? Unfortunately, the man pages and FAQ did not lead me to an > obvious solution. > > Any help appreciated.> str(as.Date("2004-01-13"))Class 'Date' num 12430> as.Date("2004-01-13") - as.Date("1970-01-01")Time difference of 12430 days (Using ISO standard format, others require an explicit format= argument) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Hi
>>TODAY <- as.POSIXlt('2004-01-17')
>>LYTODAY <- as.POSIXlt('2003-01-17')
>>DAYS <- TODAY-LYTODAY
>>DAYS
Time difference of 365 days
>>DAYS[[1]]
[1] 365
perhaps it helps,
Christian
Dennis Fisher wrote:
> I have data in the following format:
>
> > DATE
> [1] "01/13/2004"
>
> In order to find the difference between two data points, I presently
> use brute force to calculate the day of the year:
>
> > strptime(DATE, format="%m/%d/%Y")$yday
> [1] 12
>
> Although this works, it may not be robust over different years. I
> assume that R is sufficiently clever that a much simpler approach
> exists, e.g., can I calculate the number of days since some fixed
> time? Unfortunately, the man pages and FAQ did not lead me to an
> obvious solution.
>
> Any help appreciated.
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-415-564-2220
> www.PLessThan.com
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>