R expss包:按统计信息格式化数字/将不同的格式应用于替代行 [英] R expss package: format numbers by statistic / apply different format to alternate rows

查看:56
本文介绍了R expss包:按统计信息格式化数字/将不同的格式应用于替代行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索expss软件包,以便完全更改R的SPSS.我的标准表格在行中显示计数和百分比,有时还添加了其他统计信息.

是否可以通过统计信息或行更改数字格式?更具体的说,我想用0位数字显示计数,用2位数字显示百分比,最好以%格式显示,用2位数字表示均值.

我在htmlTables和htmlTable.etable {expss}中进行了搜索,但找不到解决方法.

发送所有帮助

格雷戈里,

为您的利益而发送.请参见下面的小示例.

I'm exploring the expss package in order to change SPSS completely for R. My standard tabels show counts and percentages in the rows, sometimes also complemented with additional statistics.

Is there a way to change the number format by statistic or row? More concrete I would like to show the counts with 0 digits, percentages with 2 digits and ideally in % format and the means with 2 digits.

I searched in htmlTables and htmlTable.etable {expss} but am not able to find a way to do this.

Tx for all help

Hi Gregory,

Tx for your interest. See below small example.

Table as is

Table I'd like to see

Tx, micha

解决方案

Tables are usual data.frames so we can easily apply standard R formatting functions. Example:

library(expss)
data(mtcars)
mtcars = apply_labels(mtcars,
                      mpg = "Miles/(US) gallon",
                      cyl = "Number of cylinders",
                      disp = "Displacement (cu.in.)",
                      hp = "Gross horsepower",
                      drat = "Rear axle ratio",
                      wt = "Weight (1000 lbs)",
                      qsec = "1/4 mile time",
                      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"
)


# custom formating function
custom_format = function(tbl, percent_digits = 2, count_digits = 0){
    percent_rows = grepl("\\|%$", tbl[[1]], perl = TRUE) # get rows with percent format
    count_rows = grepl("\\|N$", tbl[[1]], perl = TRUE) # get rows with count format
    # format each stat
    rounded_percent = format(tbl[percent_rows,-1], digits = percent_digits, nsmall = percent_digits) 
    rounded_count = format(tbl[count_rows,-1], digits = count_digits, nsmall = count_digits)
    # replcae data in orginal tables with formatted stat
    tbl[percent_rows,-1] = rounded_percent
    tbl[count_rows,-1] = rounded_count
    ##### remove NA which arise during formatting
    recode(tbl) = perl("^\\s*NA\\s*$") ~ ""
    tbl
}


## example
expss_output_viewer()
mtcars %>% 
    tab_cells(gear) %>% 
    tab_cols(total(), am) %>% 
    tab_stat_cases(label = "N", total_row_position = "above") %>% 
    tab_stat_cpct(label = "%", total_row_position = "none") %>% 
    tab_pivot(stat_position = "inside_rows") %>% 
    custom_format()

这篇关于R expss包:按统计信息格式化数字/将不同的格式应用于替代行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆