I'm trying to pass a nested list to a spline function. I have quarterly rainfall values for two cities for two consecutive years. I am trying to execute a spline interpolation such that I may get back monthly values for those years
Here's what I tried:
# Create a dummy data-frame
df = data.frame(city_name = rep(sapply(c('city_a','city_b'), function(x) {rep(x, 8)}), 1),
period = rep(sapply(c('2016-17','2017-18'), function(x) {rep(x, 4)}), 2),
quarter = rep(sapply(c('q1','q2','q3','q4'), function(x) {rep(x, 1)}), 4),
e_date = rep(seq(zoo::as.Date('2016-12-01'),zoo::as.Date('2018-09-01'), by='quarter'),2),
x = c(5.3, 5.7, 5.6, 5.9, 5.8, 6.1, 6.2, 6.5, 5.7, 5.8, 5.5, 5.9, 5.7, 5.8, 6.1, 6.1))
# Create separate lists for each city
out <- split(df, f = df$city_name)
# Columns to exclude from the lists - way spline works I guess!
cols_to_drop = c('city_name','period','quarter')
# drop desired cols from each list
temp <- lapply(out, function(x) x[,!(colnames(x) %in% cols_to_drop), drop=F])
# create a sequential placeholder for monthly dates
monthly = sort(seq(zoo::as.Date('2016-12-01'), zoo::as.Date('2018-09-01'), by="month"))
# Create lists using spline interpolation
spline_df <- data.frame(periodicity=monthly, x_est=spline(temp$city_a, method = 'fmm', xout = monthly)$y)
But instead of passing temp$city_name
how can I pass the entire list so that it returns a data frame of interpolated lists or data frames?
City names can be a column or row-index - either works