I have a wide data frame, where one column has a variable name and the other columns have measurements. For each cell in a column I would like to paste in the percentage value that cell contributes to the whole coloumn. So for example, I would like to take this:
Ship Month1 Month2 Month3 Month4
A 5 10 5 10
B 7 30 5 0
C 3 5 5 0
D 5 5 5 0
And have in it's place something like this:
Ship Month1 Month2 Month3 Month4
A 5 (25%) 10 (20%) 5 (25%) 10 (100%)
B 7 (35%) 30 (60%) 5 (25%) 0 (0%)
C 3 (15%) 5 (10%) 5 (25%) 0 (0%)
D 5 (25%) 5 (10%) 5 (25%) 0 (0%)
How would I be able to go around doing that?
CodePudding user response:
Base R:
cols = grep('Month', names(df))
df[cols] <- lapply(df[cols], \(x) sprintf("%d (%s%%)", x, x / sum(x) * 100))
# Ship Month1 Month2 Month3 Month4
# 1 A 5 (25%) 10 (20%) 5 (25%) 10 (100%)
# 2 B 7 (35%) 30 (60%) 5 (25%) 0 (0%)
# 3 C 3 (15%) 5 (10%) 5 (25%) 0 (0%)
# 4 D 5 (25%) 5 (10%) 5 (25%) 0 (0%)
Made more concise with magrittr
:
library(magrittr)
df[cols] %<>% lapply(\(x) sprintf("%d (%s%%)", x, x / sum(x) * 100))
Reproducible data:
df = data.frame(
Ship = c("A", "B", "C", "D"),
Month1 = c( 5L, 7L, 3L, 5L),
Month2 = c(10L, 30L, 5L, 5L),
Month3 = c( 5L, 5L, 5L, 5L),
Month4 = c(10L, 0L, 0L, 0L)
)