expss 表中的条件格式
Conditional formatting in expss tables
我的问题可以考虑扩展以下讨论:R expss package: format numbers by statistic / apply different format to alternate rows
我想了解条件的语法以便能够编写我自己的自定义格式。考虑来自数据集的 'insert' 数据框。然后我们创建以下 table 感谢 expss:
infert %>%
tab_cells(parity) %>%
### TOTAL
tab_cols(total()) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
### OTHER VARIABLES
tab_cols(education) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
tab_stat_cpct(label="%Col.", total_row_position="none") %>%
tab_pivot(stat_position="inside_columns") %>%
format_vert()
最后一行操作基本格式,如上文 URL 所述。详情:
format_vert = function(tbl, pct_digits=2, n_digits=0){
#Finding columns to format
pct_cols = grepl("\|%Col.$", names(tbl), perl = TRUE)
n_cols = grepl("\|N$", names(tbl), perl = TRUE)
#Format
recode(tbl[,-1]) = other ~ function(x) ifelse(is.numeric(x) & is.na(x), 0, x)
tbl[,pct_cols] = format(tbl[,pct_cols], digits=pct_digits, nsmall=pct_digits)
tbl[,n_cols] = format(tbl[,n_cols], digits=n_digits, nsmall=n_digits)
recode(tbl[,pct_cols]) = other ~ function(x) paste0(x, "%")
tbl
}
我了解如何设置整个 table 或列的格式(专家会注意到与 URL 中示例的区别),但如果我只想设置特定单元格的格式怎么办?例如,当 value = 100,00% 时如何设置 digits=0
(只显示 100%)?
我不知道我是否应该去 recode
、format
、何时何地参考 tbl[,pct_cols]
...
谢谢!
最简单的方法是在函数 format_vert
中的 recode
中插入额外的重新编码。我们不能使用 '100.00' ~ '100' 形式的重新编码,因为列已经与空格对齐。所以我们使用正则表达式。 perl
表示 perl 风格的正则表达式比较,\b
表示单词边界。将重新编码与此类表达式匹配的所有值。
data(infert)
format_vert = function(tbl, pct_digits=2, n_digits=0){
#Finding columns to format
pct_cols = grepl("\|%Col.$", names(tbl), perl = TRUE)
n_cols = grepl("\|N$", names(tbl), perl = TRUE)
#Format
recode(tbl[,-1]) = other ~ function(x) ifelse(is.numeric(x) & is.na(x), 0, x)
tbl[,pct_cols] = format(tbl[,pct_cols], digits=pct_digits, nsmall=pct_digits)
tbl[,n_cols] = format(tbl[,n_cols], digits=n_digits, nsmall=n_digits)
recode(tbl[,pct_cols]) = c(
perl("\b0.00\b") ~ "0% ", # additional recodings
perl("\b100.00\b") ~ "100% ", # additional recodings
other ~ function(x) paste0(x, "%")
)
tbl
}
infert %>%
tab_cells(parity) %>%
### TOTAL
tab_cols(total()) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
### OTHER VARIABLES
tab_cols(education) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
tab_stat_cpct(label="%Col.", total_row_position="none") %>%
tab_pivot(stat_position="inside_columns") %>%
format_vert()
我的问题可以考虑扩展以下讨论:R expss package: format numbers by statistic / apply different format to alternate rows
我想了解条件的语法以便能够编写我自己的自定义格式。考虑来自数据集的 'insert' 数据框。然后我们创建以下 table 感谢 expss:
infert %>%
tab_cells(parity) %>%
### TOTAL
tab_cols(total()) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
### OTHER VARIABLES
tab_cols(education) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
tab_stat_cpct(label="%Col.", total_row_position="none") %>%
tab_pivot(stat_position="inside_columns") %>%
format_vert()
最后一行操作基本格式,如上文 URL 所述。详情:
format_vert = function(tbl, pct_digits=2, n_digits=0){
#Finding columns to format
pct_cols = grepl("\|%Col.$", names(tbl), perl = TRUE)
n_cols = grepl("\|N$", names(tbl), perl = TRUE)
#Format
recode(tbl[,-1]) = other ~ function(x) ifelse(is.numeric(x) & is.na(x), 0, x)
tbl[,pct_cols] = format(tbl[,pct_cols], digits=pct_digits, nsmall=pct_digits)
tbl[,n_cols] = format(tbl[,n_cols], digits=n_digits, nsmall=n_digits)
recode(tbl[,pct_cols]) = other ~ function(x) paste0(x, "%")
tbl
}
我了解如何设置整个 table 或列的格式(专家会注意到与 URL 中示例的区别),但如果我只想设置特定单元格的格式怎么办?例如,当 value = 100,00% 时如何设置 digits=0
(只显示 100%)?
我不知道我是否应该去 recode
、format
、何时何地参考 tbl[,pct_cols]
...
谢谢!
最简单的方法是在函数 format_vert
中的 recode
中插入额外的重新编码。我们不能使用 '100.00' ~ '100' 形式的重新编码,因为列已经与空格对齐。所以我们使用正则表达式。 perl
表示 perl 风格的正则表达式比较,\b
表示单词边界。将重新编码与此类表达式匹配的所有值。
data(infert)
format_vert = function(tbl, pct_digits=2, n_digits=0){
#Finding columns to format
pct_cols = grepl("\|%Col.$", names(tbl), perl = TRUE)
n_cols = grepl("\|N$", names(tbl), perl = TRUE)
#Format
recode(tbl[,-1]) = other ~ function(x) ifelse(is.numeric(x) & is.na(x), 0, x)
tbl[,pct_cols] = format(tbl[,pct_cols], digits=pct_digits, nsmall=pct_digits)
tbl[,n_cols] = format(tbl[,n_cols], digits=n_digits, nsmall=n_digits)
recode(tbl[,pct_cols]) = c(
perl("\b0.00\b") ~ "0% ", # additional recodings
perl("\b100.00\b") ~ "100% ", # additional recodings
other ~ function(x) paste0(x, "%")
)
tbl
}
infert %>%
tab_cells(parity) %>%
### TOTAL
tab_cols(total()) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
### OTHER VARIABLES
tab_cols(education) %>%
tab_stat_cases(label="N", total_row_position="none") %>%
tab_stat_cpct(label="%Col.", total_row_position="none") %>%
tab_pivot(stat_position="inside_columns") %>%
format_vert()