Home > OS >  Replace NAs in one matrix with values in corresponding positions from another matrix
Replace NAs in one matrix with values in corresponding positions from another matrix

Time:03-30

I have one data matrix that has missing values marked as NA:

dt1 = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 0.1489, 1, NA, 
1, 0.4598, 0.254, NA, 0.258, 1, 1, 0.36, 1, 0.214, 1, 0.3547, 
1, 0.254, NA, 1, 0.258, NA, 1, 0.258, 1, 0.258, 1, 0.855, NA, 
1, 1, 1, 1, 1, NA, NA, 1, 1, 1, 0.247, NA, 1, 1, 0.257, 1, 0.254, 
1, 1, 1, 1), .Dim = c(10L, 6L), .Dimnames = list(NULL, c("ID", 
"2001", "2002", "2003", "2004", "2005")))

I have another data matrix (dt2) of the same structure that I want to use to replace the NAs in the first matrix with values that are in corresponding positions to dt1:

dt2 = structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 1, 1, 0.558, 1, 
1, 1, 0.258, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.26, 1, 1, 0.69, 
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0.256, 0.778, 1, 1, 1, 1, 
0.25, 1, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(10L, 6L), .Dimnames = list(
    NULL, c("ID", "2001", "2002", "2003", "2004", "2005")))

Looking at this, I thought I would be able to do this relatively simply:

dt1[is.na(dt1)] <- dt2

But this produces:

      ID   2001   2002  2003  2004  2005
 [1,]  1 1.0000 1.0000 0.258 1.000 8.000
 [2,]  2 0.1489 0.3600 4.000 1.000 1.000
 [3,]  3 1.0000 1.0000 1.000 1.000 1.000
 [4,]  4 1.0000 0.2140 0.258 1.000 0.257
 [5,]  5 1.0000 1.0000 1.000 6.000 1.000
 [6,]  6 0.4598 0.3547 0.258 7.000 0.254
 [7,]  7 0.2540 1.0000 1.000 1.000 1.000
 [8,]  8 2.0000 0.2540 0.855 1.000 1.000
 [9,]  9 0.2580 3.0000 5.000 1.000 1.000
[10,] 10 1.0000 1.0000 1.000 0.247 1.000

My desired output is:

expected = structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), `2001` = c(1, 
0.1489, 1, 0.558, 1, 0.4598, 0.254, 0.258, 0.258, 1), `2002` = c(1, 
0.36, 1, 0.214, 1, 0.3547, 1, 0.254, 0.26, 1), `2003` = c(0.258, 
0.69, 1, 0.258, 1, 0.258, 1, 0.855, 0, 1), `2004` = c(1, 1, 1, 
1, 0.256, 0.778, 1, 1, 1, 0.247), `2005` = c(0.25, 1, 1, 0.257, 
1, 0.254, 1, 1, 1, 1)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))

Any help is greatly appreciated!

CodePudding user response:

Use:

dt1[is.na(dt1)] <- dt2[is.na(dt1)]

dt1

#>       ID   2001   2002  2003  2004  2005
#>  [1,]  1 1.0000 1.0000 0.258 1.000 0.250
#>  [2,]  2 0.1489 0.3600 0.690 1.000 1.000
#>  [3,]  3 1.0000 1.0000 1.000 1.000 1.000
#>  [4,]  4 0.5580 0.2140 0.258 1.000 0.257
#>  [5,]  5 1.0000 1.0000 1.000 0.256 1.000
#>  [6,]  6 0.4598 0.3547 0.258 0.778 0.254
#>  [7,]  7 0.2540 1.0000 1.000 1.000 1.000
#>  [8,]  8 0.2580 0.2540 0.855 1.000 1.000
#>  [9,]  9 0.2580 0.2600 0.000 1.000 1.000
#> [10,] 10 1.0000 1.0000 1.000 0.247 1.000
  •  Tags:  
  • r
  • Related