[R] interpolation function

tom wright tom at maladmin.com
Fri Aug 5 13:10:59 CEST 2005


On Fri, 2005-05-08 at 12:12 +0000, å´ æ˜Š wrote:
> Hi,
> I have a sparse matrix.I want to fill values into the entries whose value 
> is 0.The new generated values should come from the interpolation of the 
> values have existed.Does R provide such interpolation functions which 
> operate on Matrix, for example ,such a matrix below
>  0  0  0  0  2.3  0 0  0  0 
>  0  0 3.1 0   0   0 0 1.4 0
>  0  0  0  0   0   0 0  0  0
> 1.1 0  0  0   0   0 0  0  0
>  0  0  0  4   0   0 0  0  6
>  0  0  0  0   0   0 0  0  0
>  0  0  0  0   0   7 0  0  0
>  0  3  0  0   0   0 6  0  0
>  0  0  0  0   9   0 0  0  0
> thanks a lot
> hao wu

Does this look like an answer? If anyone knows a better way please tell
me.

> d<-c(0,2,3,2,0,3,4,0,0,0,0,0)
> d.mat<-matrix(data=d,nrow=4,ncol=3,byrow=TRUE)
> for(i in 1:length(d.mat[1,])){
+ d.mat[,i][d.mat[,i]==0]<-mean(d.mat[,i][d.mat[,i]>0])
+ }

Thanks
Tom




More information about the R-help mailing list