1

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))
Hydro
  • 1,057
  • 12
  • 25

2 Answers2

1

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)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks @akrun, how would we get this into a data.frame where 1st column is `Dates`, and the other one is `Flow` (in sequence ie., first 2009 data, followed by 2012, and so on)? – Hydro Jul 10 '20 at 00:32
  • @Hydro, Use `do.call(rbind, lst1)` – akrun Jul 10 '20 at 00:41
1

Here is a possible solution:

set.seed(123)
sample_size <- 1000

y <- sample(c(2009,2012,2015,2019),sample_size,replace=TRUE)
simulate_date <- function(year){
  n_days <- ifelse(lubridate::leap_year(year),
                   366,365)
  as.Date(sample(1:n_days, 1), origin=paste0(year,"-01-01"))
}

dates <- Reduce(`c`, purrr::map(y, simulate_date))

> head(dates)
[1] "2012-06-28" "2012-01-15" "2009-07-15" "2012-11-02" "2019-04-29"
[6] "2015-10-27"
slava-kohut
  • 4,203
  • 1
  • 7
  • 24