【发布时间】:2020-05-26 16:30:57
【问题描述】:
我有两个数据集,时间步长 t 和高度 h,我合并了。
dataset_a <- data.table(t=rep(c(1,2,3,4,5,6,7,8,9), each=5),
h=rep(c(1:5)),
v=c(1:(5*9)))
一个有测量差距,以及我们实际测量但没有测量的值。
dataset_b <- data.table(t=rep(c(1,2,4,5,6,8,9), each=5),
h=rep(c(1:5)),
w=c(1:(5*7)))
dataset_b$w[12:20] <-0
合并:
dataset_merged <- merge(dataset_a, dataset_b, all=TRUE, by = c('t', 'h'))
现在我想填补空白。如何告诉 data.table 使用相邻值来填充像素?
dataset_merged[is.na(w),
w:= mean(c(the value at this h one timestep earlier, the value at this h one timestep later))]
非常感谢!
编辑 在 Bens 非常有帮助的评论之后,我不得不调整可重现的示例: 他的解决方案有效,但如果缺少“框架”数据则无效: 如果
dataset_b <- data.table(t=rep(c(2,4,5,6,8,9), each=5),
h=rep(c(1:5)),
w=c(1:(5*6)))
#removed the first timestep in this case
dataset_merged <- merge(dataset_a, dataset_b, all=TRUE, by = c('t', 'h'))
library(zoo)
dataset_merged[order(h,t)][, w := na.approx(w)]
产量
Error in `[.data.table`(dataset_merged[order(h, t)], , `:=`(w, na.approx(w))) :
Supplied 44 items to be assigned to 45 items of column 'w'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.
将它们保留为 NA 是可以的,但我如何让函数清楚地知道这一点? 不幸的是,原始数据不在规则网格上。
【问题讨论】:
-
嗨,本,非常感谢。看起来这个简单的解决方案可以为这个小样本完成工作。我将整夜运行实际数据。但我很有希望。如果您将其发布为答案,我可以将其标记为正确。
-
也许你能帮我解释一下 na.approx 如何处理一个有多个 NA 的案例?当为我的数据使用您的解决方案时,我得到
Error in `[.data.table`(dataset[order(height, datetime)], , `:=`(ze, na.approx(ze))) : Supplied 16885434 items to be assigned to 16888965 items of column 'ze'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.由于输入与输出的值相同,我不明白这种差异可能来自哪里。
标签: r data.table interpolation