[R] add trailing dates with rbind
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Wed Aug 12 14:10:28 CEST 2020
Hi Frederik,
(short answer) modify the assignment statement to
agg_d_h <- rbind( agg_d_h, data.frame(Group.date=next_date,x=0) )
Note: replace x=0 by your-variable-name=0
Note: left-hand-side of the assignment statement should be agg_d_h
(longer answer) Your approach is far from the best way to do this task, for
a variety of reasons.
If you think that in the future you will be working a lot with daily time
series and need to perform similar tasks, I would strongly recommend
learning the xts data structure in the xts package.
If you have several time series with different date ranges, and all of them
are xts objects, you can merge them with 'joins' (left joins, right joins,
full joins). xts will automatically handle alignment
and preserving dates, etc.
HTH,
Eric
On Wed, Aug 12, 2020 at 2:44 PM Frederik Feys <frefeys using gmail.com> wrote:
> I am having a hell of a time, this must surely be simple to solve….
>
> Basically I want to add trailing dates to datasets with differing starting
> dates so that across datasets I have the same starting date.
>
> # make dataset with the same starting date
> start_date = as.Date("2020-03-01")
> d_start_date = min(agg_d_h$Group.date)
>
> diff_in_days = as.numeric(difftime(d_start_date, start_date, units =
> "days"))
>
> for(i in 1:diff_in_days) {
> next_date = start_date+i
> app_d <- rbind(agg_d_h, c(next_date, 0) )
> }
>
> gives:
> Error in as.Date.numeric(value) : 'origin' must be supplied
>
> Thank you for your time to help me!
>
> Frederik Feys
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list