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

columnwisePercentage(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 9 1 68 #> B 83 45 71 #> C 59 47 79 #> D 16 27 97
columnwisePercentage(M1)
#> 1 2 3 #> A 5.4 0.8 21.6 #> B 49.7 37.5 22.5 #> C 35.3 39.2 25.1 #> D 9.6 22.5 30.8
M2
#> 1 2 3 #> A NA NA 68 #> B 83 45 71 #> C 59 47 79 #> D NA 27 97
columnwisePercentage(M2)
#> 1 2 3 #> A 0.0 0.0 21.6 #> B 58.5 37.8 22.5 #> C 41.5 39.5 25.1 #> D 0.0 22.7 30.8
columnwisePercentage(M2, default = 0)
#> 1 2 3 #> A 0.0 0.0 21.6 #> B 58.5 37.8 22.5 #> C 41.5 39.5 25.1 #> D 0.0 22.7 30.8