1

我正在尝试使用包中显示计数、总数和我的 NA的tab_cells命令在R 中创建一组交叉表。expss我不能让它给我 NA。

我已经尝试过使用na_if并且tab_mis_val我已经尝试过使用 cro 命令。我找到了一个我非常喜欢使用 fre 的频率表,并且我想基本上将它复制为交叉表。我也tabyljanitor包中使用过,可以获得 NA 行,但我一次只能运行一个交叉表,而不是从 var1 到 var10。

#I feel like I'm close with this

data%>%  
  tab_cells(var1 %to% var10) %>%  
  tab_cols(total(), var12) %>%  
  tab_stat_cpct() %>% 
  tab_mis_val() %>% 
  tab_pivot() 

#frequency table I really like that DOES give me NAs
expss_output_viewer()
calculate(data, fre(as.list(var1 %to% var10)))

#attempt to make it as a crosstab

expss_output_viewer()
calculate(data, cro_cases(as.list(var1 %to% var10, var12)))


#Using tabyl I can get NAs but it will only give one one crosstab at a 
# time instead of a` whole set of them.
library(janitor)

data %>% 
tabyl(var1, var12) %>% 
adorn_totals(c("row", "col")) %>% 
adorn_percentages("row") %>% 
adorn_pct_formatting() %>% 
adorn_ns() %>% 
adorn_title("combined") %>% 
knitr::kable() 

我想要一个包含计数、百分比、总行和 NA 行的表。我似乎无法获得 NA 行tab_cells

4

1 回答 1

0

if_na应该做的伎俩:

data%>%  
  tab_cells(
    add_val_lab(if_na(var1 %to% var10, 999), c("<NA>" = 999))
  ) %>%  
  tab_cols(total(), var12) %>%  
  tab_stat_cpct() %>% 
  tab_pivot() 

是的,它相当冗长,我将在未来的expss版本中对其进行简化。如果您不使用值标签,则可以删除add_val_lab函数。if_na(var1 %to% var10, '<NA>')就足够了。更新 21.05

总百分比下的代码:

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 (lb/1000)",
                      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"
)


# create function which calculate single table with NA percent under the total
tab_with_na = function(x, weight = NULL) {
    rbind(
        cro_cpct(
            list(unvr(x)),  # list(unvr()) to completely drop variable labels
            col_vars = "|",
            weight = weight), 
        # calculate percent of NA
        cro_mean(
            list("<NA>" = unvr(is.na(x)*100)),  # list(unvr()) to completely drop variable labels
            col_vars = "|",
            weight = weight)

    )
}

mtcars %>%  
    tab_cells(am %to% carb) %>%  
    tab_cols(total(), vs) %>% 
    tab_stat_fun(tab_with_na) %>% 
    tab_pivot()  
于 2019-05-17T18:54:32.423 回答