You can use percent
function from scales
. Try
df <- as.data.frame(cor(iris[,1:4]))
library(scales)
df <- apply(df, 2, percent)
df
Sepal.Length Sepal.Width Petal.Length Petal.Width
[1,] "100%" "-12%" "87%" "82%"
[2,] "-12%" "100%" "-43%" "-37%"
[3,] "87%" "-43%" "100%" "96%"
[4,] "82%" "-37%" "96%" "100%"
Note that the percent
function takes a numeric
variable as argument, not a matrix
or data.frame
. Therefore, you needed to apply
the function over each column of your Correlation_tab
.
If you want more digits, cou can design a function like this (here we use 4 digits)
percent_digits <- function(x, digits = 3, format = "f") {
paste0(formatC(100 * x, format = format, digits = digits), "%")
}
df <- apply(df, 2, percent_digits)
df
Sepal.Length Sepal.Width Petal.Length Petal.Width
[1,] "100.0000%" "-11.7570%" "87.1754%" "81.7941%"
[2,] "-11.7570%" "100.0000%" "-42.8440%" "-36.6126%"
[3,] "87.1754%" "-42.8440%" "100.0000%" "96.2865%"
[4,] "81.7941%" "-36.6126%" "96.2865%" "100.0000%"