表格格式-数值,小数,百分比

时间:2018-12-20 11:42:01

标签: r expss

我需要在5%,10%之类的值以及均值和sd的小数之后显示%符号

library(expss)
xltable=EDat %>% calc(list(
                        cro_cpct     (A1A,total())
                        cro_mean_sd_n     (N_A1A, total())
                    ))
xltable

我希望A1A的表格中包含百分比符号 和N_A1A的小数位表

1 个答案:

答案 0 :(得分:0)

您可以使用expss_digits设置所需的小数位数。 百分比格式化功能:

 ########################
    add_percent = function(x, digits = get_expss_digits(), ...){
        UseMethod("add_percent")
    } 

    add_percent.default = function(x, digits = get_expss_digits(), ...){
        res = formatC(x, digits = digits, format = "f")
        nas = is.na(x)
        res[nas] = ""
        res[!nas] = paste0(res[!nas], "%")
        res
    } 

    add_percent.etable = function(x, digits = get_expss_digits(), excluded_rows = "#", ...){
        included_rows = !grepl(excluded_rows, x[[1]], perl = TRUE)
        for(i in seq_along(x)[-1]){
            if(!is.character(x[[i]])){
                x[[i]][included_rows ] = add_percent(x[[i]][included_rows])
            }
        } 
        x
    }

    ######################
    library(expss)
    data(mtcars)
    mtcars = apply_labels(mtcars,
                          mpg = "Miles/(US) gallon|Mean",
                          cyl = "Number of cylinders",
                          disp = "Displacement (cu.in.)|Mean",
                          hp = "Gross horsepower|Mean",
                          drat = "Rear axle ratio",
                          wt = "Weight (lb/1000)",
                          qsec = "1/4 mile time|Mean",
                          vs = "Engine",
                          vs = c("V-engine" = 0,
                                 "Straight engine" = 1),
                          am = "Transmission",
                          am = c("Automatic" = 0,
                                 "Manual"=1),
                          gear = "Number of forward gears",
                          carb = "Number of carburetors"
    )


    mtcars_table = cro_cpct(list(mtcars$cyl), 
                            list(mtcars$vs %nest% mtcars$am, "#Total")) 


    add_percent(mtcars_table)
    # |                     |              |       Engine |        |                 |        | #Total |
    # |                     |              |     V-engine |        | Straight engine |        |        |
    # |                     |              | Transmission |        |    Transmission |        |        |
    # |                     |              |    Automatic | Manual |       Automatic | Manual |        |
    # | ------------------- | ------------ | ------------ | ------ | --------------- | ------ | ------ |
    # | Number of cylinders |            4 |              |  16.7% |           42.9% | 100.0% |  34.4% |
    # |                     |            6 |              |  50.0% |           57.1% |        |  21.9% |
    # |                     |            8 |       100.0% |  33.3% |                 |        |  43.8% |
    # |                     | #Total cases |           12 |      6 |               7 |      7 |     32 |