代码之家  ›  专栏  ›  技术社区  ›  Abdelmoamen

如何在具有R的表的结果上应用百分比fun

r
  •  -1
  • Abdelmoamen  · 技术社区  · 8 年前

    我有一个来自除法运算的表,结果是十进制格式,我想把它转换成百分比格式。我试着使用 percent 函数直接执行此操作,但当我尝试使用 Per <- percent(correlation_Tab) .

    这是我的标签代码:

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

    结果如下: enter image description here

    2 回复  |  直到 8 年前
        1
  •  3
  •   amrrs    8 年前

    乘以100不就是%格式吗?

    > 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
    

    要使用的扩展解决方案 scales 对于百分比,还要保留列名和行名:

    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%
    
        2
  •  1
  •   nghauran    8 年前

    您可以使用 percent 函数来自 scales . 尝试

    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%" 
    

    请注意 百分比 函数采用 numeric 变量作为参数,而不是 matrix 或 data.frame . 因此,您需要 apply 每个列上的函数 Correlation_tab

    如果你想要更多的数字,cou可以设计这样的函数(这里我们使用4位数字)

    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%"