Reputation: 652
When running the code below which is meant to create an end date for a daily time series (next day ahead), I am using c()
to combine the shifted time series (which when run on its own returns a date format) with the last end date as specified. However, rather than adding 2017-05-09
to the end of it, it returns a numerical value, which while correctly shifted is of little use.
As can be seen in the example below it also does something strange with the last date which is supposed to be added.
Why is this happening and how can I fix it?
as.Date(portsim$period)
periods <- data.frame(period = portsim$period)
periods$start <- periods$period
periods$end <- c(periods$start[-1], as.Date("2017-05-09")) ##problematic line
> periods
period start end
1 2011-08-12 2011-08-12 2
2 2011-08-15 2011-08-15 3
3 2011-08-16 2011-08-16 4
4 2011-08-17 2011-08-17 5
5 2011-08-18 2011-08-18 6
6 2011-08-19 2011-08-19 7
7 2011-08-22 2011-08-22 8
8 2011-08-23 2011-08-23 9
9 2011-08-24 2011-08-24 10
10 2011-08-25 2011-08-25 11
...
1432 2017-05-04 2017-05-04 1433
1433 2017-05-05 2017-05-05 1434
1434 2017-05-08 2017-05-08 17295
Upvotes: 0
Views: 44
Reputation: 18425
It looks as if your dates are actually character or even factors. Try str(periods)
to check.
If so, then replace your second line with
periods$start <- as.Date(as.character(periods$period))
Upvotes: 2
Reputation: 92
Adding 1 to the date should work
period <- seq(as.Date("2011-08-12", "%Y-%m-%d"), length = 7, # 1 week long
by = 1) # seq by day
start <- period
periods <- data.frame(period, start)
library(lubridate) # can do it with base R as well
periods$end <- periods$start + days(1)
Upvotes: 1