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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -