-1

I have a table which come from dividing operations and the result is in decimal format and i want to convert it to percent Format. I tried to use percent function to do that directly but there's an error appears when i try to use Per <- percent(correlation_Tab) .

This is my tab code:

Correlation_Tab <- mapply(function(Product, ID) table(Product)/length(ID),
                        Initial_Tab, ListId) 

and this's the result: enter image description here

nghauran
  • 6,648
  • 2
  • 20
  • 29
Mümin
  • 309
  • 1
  • 20
  • please don't post pictures of data. Provide a [reproducible example](http://stackoverflow.com/questions/5963269) instead. – Sotos Nov 23 '17 at 09:20

2 Answers2

3

Wouldn't multiplying by 100 get you the % format?

> Correlation_Tab <- cor(iris[,1:4])
> Correlation_Tab
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000
> Correlation_Tab <- round(Correlation_Tab*100,2)
> Correlation_Tab
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length       100.00      -11.76        87.18       81.79
Sepal.Width        -11.76      100.00       -42.84      -36.61
Petal.Length        87.18      -42.84       100.00       96.29
Petal.Width         81.79      -36.61        96.29      100.00

Extended solution to use scales for percent and also to retain the column and rownames:

library(scales)
library(purrr)

Correlation_Tab <- data.frame(cor(iris[,1:4]))

Correlation_Tab1 <- purrr::map_df(Correlation_Tab,percent)

row.names(Correlation_Tab1) <- row.names(Correlation_Tab)

Correlation_Tab1

> Correlation_Tab1
# A tibble: 4 x 4
  Sepal.Length Sepal.Width Petal.Length Petal.Width
*        <chr>       <chr>        <chr>       <chr>
1         100%        -12%          87%         82%
2         -12%        100%         -43%        -37%
3          87%        -43%         100%         96%
4          82%        -37%          96%        100%
amrrs
  • 6,215
  • 2
  • 18
  • 27
1

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%"
nghauran
  • 6,648
  • 2
  • 20
  • 29