r - Replicate NA cell locations in one data frame in another -


i have 2 entities: matrix , dataframe. matrix has numbers in cells. second data frame has na in few cells. how can ensure first matrix has na in same exact position in latter data frame.

i tried complete.case() , na.omit unfortunately removes row together. tried loop iterates column row, checks cell 0 , returns location feed indp dataframe - few errors around dimensions:

running through logic. iterates 1 column @ time row; cell contains 0, take same cell location in indp matrix , set 0 - nothing seems happening.

numzero <- 0  cols <- 0     (i in 1:ncol(ws)){   (k in 1:nrow(ws)){     if (is.na(ws[k,i])){       print(indp[i,k])       indp[i,k] <- 0         numzero <- numzero + 1     }    }   cols <- cols +1 } 

it should noted indp matrix.

added cols , numzero keep tab on number of rows , cols because keep getting following error: error in indp[i, k] : subscript out of bounds. indp , ws have same dimensions. however, col , numzero counters return cols , rows different dimensions. nothing happening indp matrix. missing something?

also attaching pictures of 2 conceptualise: indp

note 0's: ws

looking following: original matrix:

    [,1] [,2] [,3] [,4] [,5] [1,]    1    6   11   16   21 [2,]    2    7   12   17   22 [3,]    3    8   13   18   23 [4,]    4    9   14   19   24 [5,]    5   10   15   20   25 

data frame:

  v1 v2 v3 v4 v5 1  1  5  1  0 na 2  3  3  1  2  2 3  0  1  5  4 na 4  5 na  3  2  0 5 na  0  3  4  1 

i first matrix come per above - have nas in same cells:

[,1] [,2] [,3] [,4] [,5] [1,]    1    6   11   16   na [2,]    2    7   12   17   22 [3,]    3    8   13   18   na [4,]    4   na   14   19   24 [5,]   na   10   15   20   25 

further this,

m1 <- matrix(1:25, 5, 5);set.seed(25);  dat1 <- as.data.frame(matrix(sample(c(na,0:5), 5*5, replace=true), ncol=5)) numzero <- 0  cols <- 0     (i in 1:ncol(dat1)){   (k in 1:nrow(dat1)){     if (is.na(dat1[k,i])){       print(dat1[i,k])       m1[i,k] <- 0         numzero <- numzero + 1     }     else{       print(dat1[i,k])     }    }   cols <- cols +1 } 

printing cell entries @ each iteration compare against original data entities getting different figures what's in them odd thought m1[1,1], e.g. should return cell entry.

we can logical matrix is.na(dat1), extract elements in 'm1' corresponds true values logical matrix , assign na

m1[is.na(dat1)] <- na m1 #     [,1] [,2] [,3] [,4] [,5] #[1,]    1    6   11   16   na #[2,]    2    7   12   17   22 #[3,]    3    8   13   18   na #[4,]    4   na   14   19   24 #[5,]   na   10   15   20   25 

Comments