R data.table - how to interpolate missing values over FIXED rows in multiple columns -
a = data.table(c(2,na,3), c(5,na,1))
when try interpolate on missing lines
a[, approx(x = 1:.n, y = .sd, xout = which(is.na(.sd))), .sdcols = 1:2]
gives following error:
error in xy.coords(x, y) : 'x' , 'y' lengths differ
i wish following:
> v1 v2 1: 2.0 5 2: 2.5 3 3: 3.0 1
it seems x
, y
(first 2 arguments) should numeric vectors. you'll need loop through each column.. here use set()
along for-loop
update original data.table by reference.
len = 1:nrow(a) (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
Comments
Post a Comment