Форматирование таблицы - числовое, десятичное, процентное - PullRequest
0 голосов
/ 20 декабря 2018

Мне нужно показать символ% после значения, такого как 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 голосов
/ 20 декабря 2018

Вы можете установить желаемое количество десятичных знаков с помощью 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 |
...