R - Divide each value in matrix by maximum value of its row/column R - Divide each value in matrix by maximum value of its row/column database database

R - Divide each value in matrix by maximum value of its row/column


try this:

A1 = mat/apply(mat,1,max)A2 = t(t(mat)/apply(mat,2,max))result = ifelse(A1>A2,A1,A2)


Unless I've missed something, this approach looks valid too:

res = diag(mat)#names(res) = colnames(mat)       mat / outer(res, res, pmin) #            Acousmatic  Acoustic  Afro.beat Alternative    Ambient#Acousmatic       1.000 0.0880000 0.02400000   0.0240000 0.00800000#Acoustic         0.088 1.0000000 0.17427306   0.2344558 0.18745098#Afro-beat        0.024 0.1742731 1.00000000   0.1174658 0.02869247#Alternative      0.024 0.2344558 0.11746582   1.0000000 0.13911765#Ambient          0.008 0.1874510 0.02869247   0.1391176 1.00000000

Where mat is:

mat = structure(c(125L, 11L, 3L, 3L, 1L, 11L, 112398L, 1810L, 24216L, 3824L, 3L, 1810L, 10386L, 1220L, 298L, 3L, 24216L, 1220L, 103286L, 2838L, 1L, 3824L, 298L, 2838L, 20400L), .Dim = c(5L, 5L), .Dimnames = list(    c("Acousmatic", "Acoustic", "Afro-beat", "Alternative", "Ambient"    ), c("Acousmatic", "Acoustic", "Afro.beat", "Alternative",     "Ambient")))


Try this code:

maxcol <- Rfast::colMaxs(x)maxrow <- Rfast::rowMaxs(x)Rfast::eachrow(x, min(maxcol, maxrow), oper = "/")