0

I need to show the % symbol after the value like 5%,10% as well as the Decimal values for mean and sd

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

I am expecting Table with % percentage Symbol for A1A and Table with Decimal places for N_A1A

Divya
  • 47
  • 7

1 Answers1

0

You can set desired number of decimal places with expss_digits. Function for percent formatting:

 ########################
    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 |
Gregory Demin
  • 4,596
  • 2
  • 20
  • 20
  • Receiving the error:Error during wrapup: default method not implemented for type 'list' after running the following "add_percent(xltable)" – Divya Dec 20 '18 at 14:10
  • @Divya You should apply it on cro_cpct, not on list. – Gregory Demin Dec 20 '18 at 14:53
  • xltable=Reckon %>% calc(list( cro_cpct (Centre,banner,total_row_position = c("above")))) add_percent(xltable) This is what i am using – Divya Dec 21 '18 at 05:52
  • @Divya It should be `xltable=Reckon %>% calc(list( cro_cpct (Centre,banner,total_row_position = c("above")) %>% add_percent()))` – Gregory Demin Dec 22 '18 at 15:47
  • 1
    'add_percent = function(x, digits = get_expss_digits()){ UseMethod("add_percent") } xltable=Reckon %>% calc(list( cro_cpct (Centre,banner,total_row_position = c("above"))%>% add_percent()))'I receive the following error:Error during wrapup: no applicable method for 'add_percent' applied to an object of class "c('etable', 'data.frame')" – Divya Dec 28 '18 at 09:45
  • @Divya It seems you didn't run code for `add_percent.default` and `add_percent.etable`. – Gregory Demin Dec 28 '18 at 21:18