(no subject)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

(no subject)

Dan Murphy
Dear Claudia:

Your question has many layers.

Technically speaking, when I do "2011-05-01 CEST" -"2011-04-01 CEST" I get:

> "2011-05-01 CEST" -"2011-04-01 CEST"
Error in "2011-05-01 CEST" - "2011-04-01 CEST" :
  non-numeric argument to binary operator

As previous posters suggest, storing the date strings as Date objects
works because Date objects work in units of days:
> d1 <- as.Date("2011-05-01 CEST")
> d0 <- as.Date("2011-04-01 CEST")
> d1-d0
Time difference of 30 days
> d1-30
[1] "2011-04-01"

R's POSIX objects conform to global time standards and therefore store
their values in units of seconds (and arguably fractions thereof):
> d1 <- as.POSIXct("2011-05-01 CEST")
> d0 <- as.POSIXct("2011-04-01 CEST")
> d1-d0
Time difference of 30 days
> d1-30
[1] "2011-04-30 23:59:30 PDT"
i.e., 30 seconds before midnight.

To subtract time from POSIX objects in units of days, you can use a
difftime object:
> d1-as.difftime(30, units="days")
[1] "2011-04-01 PDT"

One "day" does not necessarily equal 60*60*24 seconds because of leap
seconds and because of daylight savings time in certain regions.
Luckily, the POSIX standard accounts for those differences so you
should be more confident in using a difftime object for day operations
on POSIX objects rather than the 60*60*24 approximation.

The longest time units with which difftime objects work is "weeks".
"months" is an ambiguous unit because a month is not always 30 days or
4 weeks; "years" is ambiguous because a year is not always 365 days or
52 weeks or 12 months.

To accommodate "months" and "years", the mondate package on CRAN
defines 'mondate' objects that store dates in units of the fraction of
the month that the close of business on that date represents relative
to the month it is in. Its primary users are financial analysts. There
are methods to convert between Dates, POSIX objects and mondate's.
Here's your example with that package:
> library(mondate)
> d1 <- mondate("2011-05-01 CEST")
> d1 - 1 # subtract one month
mondate: timeunits="months"
[1] 2011-04-01

Regards,

Dan Murphy


*Message: 1
*Date: Tue, 30 Oct 2012 11:25:39 +0100
*From: paladini <[hidden email]>
*To: <[hidden email]>
*Subject: [R] subtract a time period from a date*
*Message-ID: <[hidden email]>
*Content-Type: text/plain; charset=UTF-8; format=flowed*
*
*Hello everybody,
*how can I reduce e.g. 30 days from a date?*
*When I do  the following  "2011-05-01 CEST" -"2011-04-01 CEST" I get:
*"Time difference of 30 days"
*an thats fine.
*
*But when I try "2011-05-01 CEST" - 30 I get nonsense.
*So how can I subtract some days, month or years from a date?
*
*
*
*thanking you in anticipation
*
*
*Claudia Paladini

______________________________________________
[hidden email] 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.