[R] [External] conditional replacement of elements of matrix with another matrix column
Richard M. Heiberger
rmh @end|ng |rom temp|e@edu
Wed Sep 1 23:12:11 CEST 2021
> A
[,1] [,2]
[1,] 12 NA
[2,] 12 NA
[3,] 12 NA
[4,] 13 NA
[5,] 13 NA
[6,] 13 NA
[7,] 14 NA
[8,] 14 NA
[9,] 14 NA
> B
[,1] [,2]
[1,] 11 6
[2,] 11 7
[3,] 11 8
[4,] 13 9
[5,] 13 10
[6,] 13 11
[7,] 14 12
[8,] 14 13
[9,] 14 14
> C
[,1] [,2]
[1,] 12 NA
[2,] 12 NA
[3,] 12 NA
[4,] 13 9
[5,] 13 10
[6,] 13 11
[7,] 14 12
[8,] 14 13
[9,] 14 14
> same <- A[,1] == B[,1]
> same
[1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
> A[same,2] <- B[same,2]
> A
[,1] [,2]
[1,] 12 NA
[2,] 12 NA
[3,] 12 NA
[4,] 13 9
[5,] 13 10
[6,] 13 11
[7,] 14 12
[8,] 14 13
[9,] 14 14
>
> On Sep 01, 2021, at 16:59, Eliza Botto <eliza_botto using outlook.com> wrote:
>
>> dput(A)
>
> structure(c(12, 12, 12, 13, 13, 13, 14, 14, 14, NA, NA, NA, NA,
> NA, NA, NA, NA, NA), .Dim = c(9L, 2L))
>
>> dput(B)
>
> structure(c(11, 11, 11, 13, 13, 13, 14, 14, 14, 6, 7, 8, 9, 10,
> 11, 12, 13, 14), .Dim = c(9L, 2L))
>
>> dput(C)
>
> structure(c(12, 12, 12, 13, 13, 13, 14, 14, 14, NA, NA, NA, 9,
> 10, 11, 12, 13, 14), .Dim = c(9L, 2L))
More information about the R-help
mailing list