Search code examples
rdataframetime-seriestidyverserandom-data

How to construct a random data set of different year in R?


The code below will generate uniformly distributed data at a daily time step for the year 2009. Suppose, i want to construct a similar data set which would include the year 2009,2012, 2015, and 2019, how would i do that?. I am basically trying to avoid repeating the code or using filter to grab data for the year of interest.

library(tidyverse)
library(lubridate)

set.seed(500)
DF1 <- data.frame(Date = seq(as.Date("2009-01-01"), to = as.Date("2009-12-31"), by = "day"),
                  Flow = runif(365,20,60))

Solution

  • Here is an option where we create a vector of year, loop over the vector, get the sequence of dates after converting to Date class and create the 'Flow' from uniform distribution

    year <- c(2009, 2012, 2015, 2019)
    lst1 <- lapply(year, function(yr) {
         dates <- seq(as.Date(paste0(yr, '-01-01')), 
                      as.Date(paste0(yr, '-12-31')), by = 'day')
         data.frame(Date = dates, 
          Flow= runif(length(dates), 20, 60))
       })
    

    and create a single data.frame with do.call

    dat1 <- do.call(rbind, lst1)