Here my time period range:
start_day = as.Date('1974-01-01', format = '%Y-%m-%d')
end_day = as.Date('2014-12-21', format = '%Y-%m-%d')
df = as.data.frame(seq(from = start_day, to = end_day, by = 'day'))
colnames(df) = 'date'
I need to created 10,000 data.frames with different fake years of 365days each one. This means that each of the 10,000 data.frames needs to have different start and end of year.
In total df
has got 14,965 days which, divided by 365 days = 41 years. In other words, df
needs to be grouped 10,000 times differently by 41 years (of 365 days each one).
The start of each year has to be random, so it can be 1974-10-03, 1974-08-30, 1976-01-03, etc... and the remaining dates at the end df
need to be recycled with the starting one.
The grouped fake years need to appear in a 3rd col of the data.frames.
I would put all the data.frames into a list but I don't know how to create the function which generates 10,000 different year's start dates and subsequently group each data.frame with a 365 days window 41 times.
Can anyone help me?
@gringer gave a good answer but it solved only 90% of the problem:
dates.df <- data.frame(replicate(10000, seq(sample(df$date, 1),
length.out=365, by="day"),
simplify=FALSE))
colnames(dates.df) <- 1:10000
What I need is 10,000 columns with 14,965 rows made by dates taken from df
which need to be eventually recycled when reaching the end of df
.
I tried to change length.out = 14965
but R does not recycle the dates.
Another option could be to change length.out = 1 and eventually add the remaining df
rows for each column by maintaining the same order:
dates.df <- data.frame(replicate(10000, seq(sample(df$date, 1),
length.out=1, by="day"),
simplify=FALSE))
colnames(dates.df) <- 1:10000
How can I add the remaining df
rows to each col?
Try this one, using subsetting instead:
Now, I create a vector long enough so that I can use easy subsetting later on:
Now, create the random start dates for 100 instances (replace this with 10000 for your application):
Now, create a list of dates by simply subsetting
date_vec2
with your desired length:The
seq
method also works if theto
argument is unspecified, so it can be used to generate a specific number of days starting at a particular date:When used in combination with
replicate
andsample
, I think this will give what you want in a list:Without the
simplify=FALSE
argument, it produces an array of integers (i.e. R's internal representation of dates), which is a bit trickier to convert back to dates. A slightly more convoluted way to do this is and produce Date output is to usedata.frame
on the unsimplifiedreplicate
result. Here's an example that will produce a 10,000-column data frame with 365 dates in each column (takes about 5s to generate on my computer):To get the date wraparound working, a slight modification can be made to the original data frame, pasting a copy of itself on the end:
This is easier to code for downstream; the alternative being a double
seq
for each result column with additional calculations for the start/end andif
statements to deal with boundary cases.Now instead of doing date arithmetic, the result columns subset from the original data frame (where the arithmetic is already done). Starting with one date in the first half of the frame and choosing the next 14965 values. I'm using
nrow(df)/2
instead for a more generic code:This takes a bit less time now, presumably because the date values have been pre-caclulated.