我有一个数据表:
library(data.table)
dt.tst <- CJ(Type = c("A", "B"),
Range_val = seq(0,20000, by = 1000))
dt.tst[Range_val == 2000 & Type == "A", Value := 0.987]
dt.tst[Range_val == 2000 & Type == "B", Value := 1.987]
dt.tst[Range_val == 9000 & Type == "A", Value := 1.056]
dt.tst[Range_val == 9000 & Type == "B", Value := 2.138]
dt.tst[Range_val == 16000 & Type == "A", Value := 1.563]
dt.tst[Range_val == 16000 & Type == "B", Value := 2.089]
我想在Value-栏中填写NA::
- 通过Range_Val列和类型使用最接近的非NA值.
- 在平局的情况下,应该使用最大的Range_Val及其相应值(但这不是交易的 destruct 者,速度更重要).
我可以用for循环来做这件事,这很简单,但速度很慢.因此,我想要一种更干净的方式(也更快).什么是一个好的数据表-这样做的方式?
始终存在非NA值,但间隔可能不同.