0
votes

I want to generate the following sequence on Lubridate

seq(ymd('2017-03-12'),ymd('2020-02-23'), by = '1 week')

and store the weeks generated as a column in an existing data.frame of 102 rows.

When I try to use dataframe["newcolumnname] <- seq(ymd('2017-03-12'),ymd('2020-02-23'), by = '1 week')

It throws an error: Error in [<-.data.frame(* tmp *, "Year", value = c(17237, 17244, 17251, : replacement has 155 rows, data has 102

What's the most efficient way to generate a sequence of dates in a column in R?

1
Really new to R. So would greatly appreciate any help. - NL91
Anything and everything about time series analysis in R can be found here: otexts.com/fpp2 - Reeza

1 Answers

0
votes

dates <- seq(from=as.Date("2017-03-12"), to=as.Date("2020-02-23"), by = "weeks") works fine. I think your problem is the length of the dataframe you're trying to fit the data in (as stated in the error).

EDIT: knowing that the goal is to have the dataframe fit the new column size, here's my suggestion: Using the method cbind.fill() from the package rowr

cbind.fill(a,b, fill = NA)

Will fill the shortest of the two elements with NA's

BTW, I would definitely recommand checking out the xts package to play around with timeseries in R.