Add percentage sign to dataframe values

Viewed 1155

I have a quick question. I have a dataframe that I'm using in a dashboard which shows rowwise percentages. I've calculated this using the (second line of) code below, but I would really like a percentage sign after each row (except for the row 'count' of course). It's just for visual purposes of the dashboard, so I've tried finding out if I can just paste/paste0 the '%'-sign in there, but without luck. Any suggestions? Much appreciated!

Dataframe:

df <- as.data.frame(matrix(sample(seq(0,40), 20, replace=T ), 4, 5))
df <- rowPercents(df, digits = 0)
2 Answers

You can paste the % sign to df :

df[] <- paste0(as.matrix(df), '%')
df
#   V1  V2  V3  V4  V5
#1  2% 28%  0%  3% 14%
#2 32% 16%  1% 20%  0%
#3  4% 36% 32% 26% 31%
#4 14% 38% 16%  9% 40%

We can use lapply as this would be more efficient column wise

df[] <- lapply(df, paste0, '%')
Related