rain_per_month <- c("jan" = 1.5, "feb" = 2.2,
"mar" = 3.0, "apr" = 4.3,
"may" = 5.75, "jun" = 6.2,
"jul" = 6.1, "aug" = 5.8,
"sep" = 4.7, "oct" = 3.7,
"nov" = 2.75, "dec" = 2.2)
df <- data.frame(my_dates = seq(as.Date("2015-01-01"), as.Date("2015-12-31"), by="days"))
I'm wondering about my options for interpolation of data among dates.
Suppose I have some some data that is averaged/ aggregated by month:
rain_per_month <- c("jan" = 1.5, "feb" = 2.2,
"mar" = 3.0, "apr" = 4.3,
"may" = 5.75, "jun" = 6.2,
"jul" = 6.1, "aug" = 5.8,
"sep" = 4.7, "oct" = 3.7,
"nov" = 2.75, "dec" = 2.2)
suppose moreover that I wish to assume the data is smooth and continuous
My Questions
Suppose I wished to assume the rain_per_month values fell in the middle of each month.
if I created a DAILY date vector, like so:
1- How could I populate my data frame with a new variable, the sparse, middle month values from the rain_per_month vector
2- What are options for applying an interpolation technique at a daily granularity, and how could this be done?
Thank you for any help
--
You received this message because you are subscribed to the Google Groups "manipulatr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to manipulatr+...@googlegroups.com.
To post to this group, send email to manip...@googlegroups.com.
Visit this group at http://groups.google.com/group/manipulatr.
For more options, visit https://groups.google.com/d/optout.
# Function to generate middle-of-the-month nodes
mid_month <- function(month_in_text) {
lubridate::yday(ymd(paste("2015", month_in_text, "15"))) ## Not a leap year
}
days <- unlist(lapply(names(rain_per_month), mid_month))
f <- splinefun(days, rain_per_month, method = "periodic")
df$rain <- f(lubridate::yday(df$my_dates))