Reputation: 1117
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))
Upvotes: 1
Views: 78
Reputation: 887711
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)
Upvotes: 1
Reputation: 4233
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"
Upvotes: 1