Calculate the percentage (value divided by sum of values in the row) for each row

rowwisePercentage(x, default = 0, digits = 1)

Arguments

x

two dimensional numeric data structure

default

default value to be used if the calculated percentage is NA.

digits

number of digits (default: 1) to which the resulting percentages are to be rounded. Set to NA to suppress rounding

Examples

# Create a random matrix of integer values M1 <- matrix(sample(100, 12), nrow = 4, dimnames = list(LETTERS[1:4], 1:3)) # Introduce some NA values <- as.numeric(M1) values[sample(length(values), 3)] <- NA M2 <- matrix(values, nrow = nrow(M1), dimnames = dimnames(M1)) M1
#> 1 2 3 #> A 95 85 70 #> B 25 29 98 #> C 77 93 69 #> D 28 55 38
rowwisePercentage(M1)
#> 1 2 3 #> A 38.0 34.0 28.0 #> B 16.4 19.1 64.5 #> C 32.2 38.9 28.9 #> D 23.1 45.5 31.4
M2
#> 1 2 3 #> A NA 85 70 #> B NA 29 98 #> C 77 NA 69 #> D 28 55 38
rowwisePercentage(M2)
#> 1 2 3 #> A 0.0 54.8 45.2 #> B 0.0 22.8 77.2 #> C 52.7 0.0 47.3 #> D 23.1 45.5 31.4
rowwisePercentage(M2, default = 0)
#> 1 2 3 #> A 0.0 54.8 45.2 #> B 0.0 22.8 77.2 #> C 52.7 0.0 47.3 #> D 23.1 45.5 31.4