【问题标题】:Statistical Summary for categorical variables - academic paper分类变量的统计摘要 - 学术论文
【发布时间】:2019-08-05 02:20:17
【问题描述】:

我正在为 R 中的分类变量准备一个汇总统计数据,以放入学术论文中。我正在寻找这样的输出:

Create summary table of categorical variables of different lengths 但是,我找不到分类变量的函数。

这是我的小例子:

library(dplyr)
library(stargazer)

mtcars %>%
  mutate(mpg_cat = ifelse(mpg > mean(mpg), 1,0)) %>%
  mutate(mpg_cat= as.factor(mpg_cat)) %>%
  mutate(cyl_cat= as.factor(cyl)) %>%
  select(cyl_cat, mpg_cat ) %>%
  function() %>% ##???
  stargazer(summary=FALSE, rownames=FALSE,
            #note you have to specify type
            type = "html",
            #note that the argument is "out" not "file"
            out="temp.doc")

这是我脑海中的输出: https://i.stack.imgur.com/CIdIa.jpg

【问题讨论】:

  • 您是否尝试过您分享的链接中给出的答案?
  • 是的。没有一个函数适用于分类变量。
  • 还有几个问题:(i)为什么注释是function() %>% ##????; (ii) 您需要的最终输出是什么?比如什么类型的汇总统计,你需要它是什么格式?

标签: r function summary


【解决方案1】:

假设您有数据来填充这样的模板,请使用库kableExtra

https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf.

(见第 14-20 页)

你最好学习如何处理表格,这个库也使用你已经知道的%>%符号。

【讨论】:

    【解决方案2】:

    我想出了这段代码:

    mtcars %>%
      mutate(mpg_cat = ifelse(mpg > mean(mpg), "Yes","No")) %>%
      mutate(mpg_cat= as.factor(mpg_cat)) %>%
      mutate(cyl_cat= as.factor(cyl)) %>%
      select(cyl_cat, mpg_cat ) %>%
    
      summary() %>%
      as.data.frame() %>%
      select(-Var1) %>%
      rename(Variable=Var2) %>%
      filter(! is.na(Freq) ) %>%
      separate(Freq, c("Level", "Freq."),sep=":" ) %>%
      mutate(Freq. = as.integer(Freq.)) %>%
      mutate(Total = nrow(mtcars)) %>%
      mutate(Perc. = Freq.*100/Total)  %>%
      select (-Total)  %>%
    
      stargazer(summary=FALSE, rownames=FALSE,
                #note you have to specify type
                type = "html",
                #note that the argument is "out" not "file"
                out="mtcars.doc")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-27
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      相关资源
      最近更新 更多