Home > Blockchain >  How to set all values except the row maximum to 0 in every row of a matrix?
How to set all values except the row maximum to 0 in every row of a matrix?

Time:10-28

Still a bit new to R and would appreciate some guidance. I have produced a relatedness matrix but before I melt it to create my edge list, I want to set all the values in each row to 0 EXCEPT for the row maximum. Any tips on how to do this?

I have no idea what to try.

CodePudding user response:

You can use apply replace:

m <- matrix(1:9, 3)
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

t(apply(m, 1, function(x) replace(x, x != max(x), 0)))
     [,1] [,2] [,3]
[1,]    0    0    7
[2,]    0    0    8
[3,]    0    0    9

or with tapply:

do.call(rbind, tapply(m, row(m), function(x) replace(x, x != max(x), 0)))

CodePudding user response:

max.col will be useful here:

m <- matrix(sample(100), 10, 10)
m
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]   93   78   98   14   11   74   63    5   38    43
#>  [2,]   34    1   55   64   67   15   97   37    4    89
#>  [3,]   52   25   75   20   77   28   65   84   39    49
#>  [4,]    3   45   79   50   18   60   48   16   13    42
#>  [5,]   24   69   91   44    9   87   17   62    2    32
#>  [6,]   95   94   68   53   99   70   82   36   66    40
#>  [7,]   92   41   23   54   30   21   88   81   72    83
#>  [8,]   59  100   56   27   10   46    6   12   26    19
#>  [9,]   90   35   71   51    8   86   76   29   31    96
#> [10,]   58   33   47   73   85   61   80   57    7    22
m[-(1:nrow(m)   (max.col(m) - 1L)*ncol(m))] <- 0
m
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    0   98    0    0    0    0    0    0     0
#>  [2,]    0    0    0    0    0    0   97    0    0     0
#>  [3,]    0    0    0    0    0    0    0   84    0     0
#>  [4,]    0    0   79    0    0    0    0    0    0     0
#>  [5,]    0    0   91    0    0    0    0    0    0     0
#>  [6,]    0    0    0    0   99    0    0    0    0     0
#>  [7,]   92    0    0    0    0    0    0    0    0     0
#>  [8,]    0  100    0    0    0    0    0    0    0     0
#>  [9,]    0    0    0    0    0    0    0    0    0    96
#> [10,]    0    0    0    0   85    0    0    0    0     0

With a numeric matrix with named rows/columns:

m <- matrix(runif(100), 10, 10, dimnames = list(LETTERS[1:10], LETTERS[11:20]))
m
#>           K          L          M         N         O         P          Q         R          S          T
#> A 0.8455340 0.97713257 0.89784675 0.9116978 0.5132513 0.4197610 0.39154932 0.5499533 0.74761367 0.04024061
#> B 0.2574573 0.42892093 0.55113837 0.1347042 0.9728912 0.2104251 0.51084878 0.6308227 0.08114070 0.08541959
#> C 0.3393108 0.14198061 0.12190379 0.2433768 0.1043313 0.2526678 0.21550723 0.5086210 0.07906360 0.88046001
#> D 0.1492551 0.66689551 0.04958598 0.3519842 0.2420030 0.5919464 0.27392652 0.2029944 0.32210388 0.90704707
#> E 0.4473961 0.61912114 0.58880869 0.9073496 0.7760235 0.7955465 0.43140215 0.5572331 0.32165581 0.90433083
#> F 0.0279498 0.86375368 0.35372818 0.5033064 0.4016708 0.6530612 0.18887117 0.3123320 0.03582699 0.27059191
#> G 0.1848169 0.11436509 0.84722483 0.8874162 0.2558677 0.6839403 0.85603179 0.5178988 0.53953932 0.55142390
#> H 0.1056349 0.48193774 0.44023254 0.5574092 0.9154315 0.6261866 0.51314697 0.5838974 0.92684901 0.75310930
#> I 0.9220023 0.86660126 0.87581045 0.2354375 0.9385472 0.3527555 0.09545731 0.1028695 0.99012272 0.71308235
#> J 0.9915948 0.04529849 0.40984050 0.4431233 0.8482269 0.1817989 0.73221712 0.6340093 0.68836208 0.69083152
m[-(1:nrow(m)   (max.col(m) - 1L)*ncol(m))] <- 0
m
#>           K         L M         N         O P Q R         S         T
#> A 0.0000000 0.9771326 0 0.0000000 0.0000000 0 0 0 0.0000000 0.0000000
#> B 0.0000000 0.0000000 0 0.0000000 0.9728912 0 0 0 0.0000000 0.0000000
#> C 0.0000000 0.0000000 0 0.0000000 0.0000000 0 0 0 0.0000000 0.8804600
#> D 0.0000000 0.0000000 0 0.0000000 0.0000000 0 0 0 0.0000000 0.9070471
#> E 0.0000000 0.0000000 0 0.9073496 0.0000000 0 0 0 0.0000000 0.0000000
#> F 0.0000000 0.8637537 0 0.0000000 0.0000000 0 0 0 0.0000000 0.0000000
#> G 0.0000000 0.0000000 0 0.8874162 0.0000000 0 0 0 0.0000000 0.0000000
#> H 0.0000000 0.0000000 0 0.0000000 0.0000000 0 0 0 0.9268490 0.0000000
#> I 0.0000000 0.0000000 0 0.0000000 0.0000000 0 0 0 0.9901227 0.0000000
#> J 0.9915948 0.0000000 0 0.0000000 0.0000000 0 0 0 0.0000000 0.0000000
  • Related