Reputation: 891
a = data.table(c(2,NA,3), c(5,NA,1))
When I try to interpolate over the missing lines
a[, approx(x = 1:.N, y = .SD, xout = which(is.na(.SD))), .SDcols = 1:2]
gives the following error:
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
I wish to get the following:
> a
V1 V2
1: 2.0 5
2: 2.5 3
3: 3.0 1
Upvotes: 3
Views: 761
Reputation: 118799
It seems like x
and y
(first two arguments) should be numeric vectors. You'll need to loop through each column.. Here I use set()
along with a for-loop
to update the original data.table by reference.
len = 1:nrow(a)
for (col in names(a)) {
nas = which(is.na(a[[col]]))
set(a, i=nas, j=col, value=approx(len, a[[col]], xout=nas)$y)
}
# V1 V2
# 1: 2.0 5
# 2: 2.5 3
# 3: 3.0 1
Upvotes: 4