Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

data.table linearly interpolating NA values without groups

$
0
0

i wanted to fill some NA values in a data.table without groups. Please consider this extract of data.table representing time and distances:

df=data.frame(time=seq(7173,7195,1),dist=c(31091.33,NA,31100.00,31103.27,NA,NA,NA,NA,31124.98,NA,31132.81,NA,NA,NA,NA,31154.19,NA,31161.47,NA,NA,NA,NA,31182.97))
DT=data.table(df)

I want in the DT data.table, to fill NA values with a function depending on non-NA value before/after. As an example, writing a function in j to replace each instruction

DT[2, dist:=(31091.33 + (31100-31091.33)/2)]

then

DT[5:8, dist:=(31103.27+"something"*(31124.98-31103.27)/5)]

etc...

Thanksfully,


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>