如何使样条函数在嵌套列表上工作

时间:2019-05-07 09:27:18

标签: r list cubic-spline

我正在尝试将嵌套列表传递给样条函数。我连续两年有两个城市的季度降雨量值。我正在尝试执行样条插值,以便可以获取那些年的月度值

这是我尝试过的:

# 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)

但是除了传递temp$city_name之外,我如何传递整个列表,以便它返回插值列表或数据帧的数据帧?

城市名称可以是列索引或行索引-可以使用

1 个答案:

答案 0 :(得分:1)

只需使用另一个db.<Collection_name>.find({}, { title: 1 })

libraryDependencies += "com.crealytics" %% "spark-excel" % "0.8.2"