[R] calculating the number of days from dates

1 view
Skip to first unread message

Bob Green

unread,
Dec 14, 2007, 7:05:19 AM12/14/07
to r-h...@r-project.org

Hello,

I gather variants of this question have been asked previously. I have
done some reading but only became more confused, as I suspect what I
am trying to do is more basic than other applications.

The following code readily calculates the difference in days between two dates:

newdays <- ISOdate(2005, 5,12) - ISOdate(2006, 12, 22)

However, I wanted to be able to deduct the dates in one variable from
the dates in another variable, resulting in a new variable - e.g the
difference in days between the two dates. Below is a sample of my
data. My questions:

1. I tried changing the data to dates via as.Date. Is this necessary
or do I need to alter the date format itself, e.g to 12/12/78 or some
other format?
2. I gather there are various packages as well as date formats.What
is the most straight forward approach to calculate the difference
between two dates, as below.


> dates <- read.csv("c:\\dates.csv",header=T)
> dates
v1 v2
1 12/12/1978 12/12/2005
2 23/01/1965 23/09/2001
3 24/12/2004 16/03/2007
4 3/03/2003 4/04/2004
5 8/11/2006 1/05/2007

> class(dates$v1)
[1] "factor"
> class(dates$v2)
[1] "factor"

> dates <- read.csv("c:\\dates.csv",header=T,
as.Date(as.character(dates) "%d/%m/%Y"))
Error: syntax error, unexpected STR_CONST, expecting ',' in "dates <-
read.csv("c:\\dates.csv",header=T, as.Date(as.character(dates) "%d/%m/%Y""
>

Any assistance is much appreciated,

Bob

______________________________________________
R-h...@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

unread,
Dec 14, 2007, 9:41:29 AM12/14/07
to Bob Green, r-h...@r-project.org
Try this:

dates <- read.csv("c:\\dates.csv",header=T)

dates[,1] <- as.Date(dates[,1], "%d/%m/%Y")
dates[,2] <- as.Date(dates[,2], "%d/%m/%Y")
transform(dates,
Dif=V2-V1)


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

Knut Krueger

unread,
Dec 15, 2007, 2:39:02 AM12/15/07
to R R-help
Bob Green schrieb:

>
> > dates <- read.csv("c:\\dates.csv",header=T)
> > dates
> v1 v2
> 1 12/12/1978 12/12/2005
> 2 23/01/1965 23/09/2001
> 3 24/12/2004 16/03/2007
> 4 3/03/2003 4/04/2004
> 5 8/11/2006 1/05/2007
>
> > class(dates$v1)
> [1] "factor"
> > class(dates$v2)
> [1] "factor"
>
>
What about chron library:

dts <- dates(c("02/27/92", "02/27/92", "01/14/92",
"02/28/92", "02/01/92"))
dts
# [1] 02/27/92 02/27/92 01/14/92 02/28/92 02/01/92
tms <- times(c("23:03:20", "22:29:56", "01:03:30",
"18:21:03", "16:56:26"))
tms
# [1] 23:03:20 22:29:56 01:03:30 18:21:03 16:56:26
x <- chron(dates = dts, times = tms)
x
# [1] (02/27/92 23:03:19) (02/27/92 22:29:56) (01/14/92 01:03:30)
# [4] (02/28/92 18:21:03) (02/01/92 16:56:26)

# We can add or subtract scalars (representing days) to dates or
# chron objects:
c(dts[1], dts[1] + 10)
# [1] 02/27/92 03/08/92
dts[1] - 31
# [1] 01/27/92


Knut

Martin Maechler

unread,
Dec 15, 2007, 5:37:29 AM12/15/07
to Knut Krueger, R R-help
>>>>> "KK" == Knut Krueger <r...@family-krueger.com>
>>>>> on Sat, 15 Dec 2007 08:39:02 +0100 writes:

KK> Bob Green schrieb:


>>
>> > dates <- read.csv("c:\\dates.csv",header=T)
>> > dates
>> v1 v2
>> 1 12/12/1978 12/12/2005
>> 2 23/01/1965 23/09/2001
>> 3 24/12/2004 16/03/2007
>> 4 3/03/2003 4/04/2004
>> 5 8/11/2006 1/05/2007
>>
>> > class(dates$v1)
>> [1] "factor"
>> > class(dates$v2)
>> [1] "factor"
>>
>>

KK> What about chron library:

it's a >> package << , not a library, please!

and as Henrique has shown it's really not needed for the question.
There's the "Date" (S3) class, and even a "difftime" one
for time *differences*
See
?as.Date
?difftime
and also note the output of
methods(class = "Date")

Martin

KK> dts <- dates(c("02/27/92", "02/27/92", "01/14/92",
KK> "02/28/92", "02/01/92"))
KK> dts
KK> # [1] 02/27/92 02/27/92 01/14/92 02/28/92 02/01/92
KK> tms <- times(c("23:03:20", "22:29:56", "01:03:30",
KK> "18:21:03", "16:56:26"))
KK> tms
KK> # [1] 23:03:20 22:29:56 01:03:30 18:21:03 16:56:26
KK> x <- chron(dates = dts, times = tms)
KK> x
KK> # [1] (02/27/92 23:03:19) (02/27/92 22:29:56) (01/14/92 01:03:30)
KK> # [4] (02/28/92 18:21:03) (02/01/92 16:56:26)

KK> # We can add or subtract scalars (representing days) to dates or
KK> # chron objects:
KK> c(dts[1], dts[1] + 10)
KK> # [1] 02/27/92 03/08/92
KK> dts[1] - 31
KK> # [1] 01/27/92


KK> Knut

KK> ______________________________________________
KK> R-h...@r-project.org mailing list
KK> https://stat.ethz.ch/mailman/listinfo/r-help
KK> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
KK> and provide commented, minimal, self-contained, reproducible code.

Knut Krueger

unread,
Dec 17, 2007, 2:10:58 AM12/17/07
to R R-help

> it's a >> package << , not a library, please!
>
>
Sorry for using library instead package, but

library() is one command for using packages.

Therefore I (and it seems that i am not the only one) used library instead package.

Knut

bogdan romocea

unread,
Dec 18, 2007, 12:46:51 PM12/18/07
to r-help, maec...@stat.math.ethz.ch
> Sorry for using library instead package, but
> library() is one command for using packages.

... which is why all efforts to make folks say "package" instead of >>
"library" << are doomed to fail, IMHO. Besides, in English, "library"
also means "a collection of software or data usually reflecting a
specific theme or application" (#9 on the list from
http://dictionary.reference.com/ ). Therefore:
> "library" == "package"
[1] TRUE!
and just about the only way to clear up the "confusion" would be to
rename library() to package(), and replace "library" with "folder" or
"directory".

Rolf Turner

unread,
Dec 18, 2007, 2:26:13 PM12/18/07
to bogdan romocea, r-help, maec...@stat.math.ethz.ch

On 19/12/2007, at 6:46 AM, bogdan romocea wrote:

>> Sorry for using library instead package, but
>> library() is one command for using packages.
>
> ... which is why all efforts to make folks say "package" instead of >>
> "library" << are doomed to fail, IMHO.

Yes, but it gives so much pleasure to those who appreciate
the distinction to rail at those who don't! :-)

cheers,

Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

Reply all
Reply to author
Forward
0 new messages