I am working on imputing missing values in a dataset. I have a predictive model which can generate the values to impute the missing. When I do imputation using dfm[is.na(dfm)]<-impute , it imputes values columnwise. But I need to impute row wise so I transposed the data matrix. My question is that, is there an elegant way to do this without transposing the matrix ? Here is an rcode with reproducible example.
set.seed(1)
r=5
c=4
df<-matrix(runif(r*c), ncol=c)
df
[,1] [,2] [,3] [,4]
[1,] 0.2655087 0.89838968 0.2059746 0.4976992
[2,] 0.3721239 0.94467527 0.1765568 0.7176185
[3,] 0.5728534 0.66079779 0.6870228 0.9919061
[4,] 0.9082078 0.62911404 0.3841037 0.3800352
[5,] 0.2016819 0.06178627 0.7698414 0.7774452
d=dim(df)
p=0.30
#### generate missing data matrix by replacing some values by NAs
dfm<-df
dfm[matrix(rbinom(prod(d), size=1,prob=p)==1,nrow=d[1])]<-NA
dfm
[,1] [,2] [,3] [,4]
[1,] NA 0.89838968 0.2059746 0.4976992
[2,] 0.3721239 0.94467527 0.1765568 NA
[3,] 0.5728534 0.66079779 0.6870228 0.9919061
[4,] 0.9082078 NA 0.3841037 NA
[5,] 0.2016819 0.06178627 NA 0.7774452
# generate values to impute the missing
impute<-rgamma(sum(is.na(dfm)),shape=1,scale=0.5)
impute
[1] 0.6804725 0.6029941 0.2770577 0.6035013 0.7812393
#imputes columnwise
dfm[is.na(dfm)]<-impute
dfm
[,1] [,2] [,3] [,4]
[1,] 0.6804725 0.89838968 0.2059746 0.4976992
[2,] 0.3721239 0.94467527 0.1765568 0.6035013
[3,] 0.5728534 0.66079779 0.6870228 0.9919061
[4,] 0.9082078 0.60299409 0.3841037 0.7812393
[5,] 0.2016819 0.06178627 0.2770577 0.7774452
#impute rowwise
tdfm<-t(dfm)
tdfm[is.na(tdfm)]<-impute
tdfm
[,1] [,2] [,3] [,4] [,5]
[1,] 0.6804725 0.3721239 0.5728534 0.9082078 0.20168193
[2,] 0.8983897 0.9446753 0.6607978 0.2770577 0.06178627
[3,] 0.2059746 0.1765568 0.6870228 0.3841037 0.78123933
[4,] 0.4976992 0.6029941 0.9919061 0.6035013 0.77744522
dfm.fill<-t(tdfm)
dfm.fill
[,1] [,2] [,3] [,4]
[1,] 0.6804725 0.89838968 0.2059746 0.4976992
[2,] 0.3721239 0.94467527 0.1765568 0.6029941
[3,] 0.5728534 0.66079779 0.6870228 0.9919061
[4,] 0.9082078 0.27705769 0.3841037 0.6035013
[5,] 0.2016819 0.06178627 0.7812393 0.7774452
set.seedto make the example reproducible. - A5C1D2H2I1M1N2O1R2T1