【问题标题】:Presentation style of the summary table in RR中汇总表的表示方式
【发布时间】:2021-03-11 03:16:13
【问题描述】:

我正在尝试在 R 中创建一个汇总表,如下表 4 所示。

现在我所拥有的类似于以下内容。我不知道如何在 R 中自动生成 Latex 代码来创建一个像上面那样的表。

我正在使用以下 R 代码来生成表格。然后我将代码复制并粘贴到 Latex 中,使其出现在 pdf 中。

#Build a rough summary table
summary1 <-
  list("Age (Years)" =
         list("Missing"  = ~ sum(is.na(df$age_years)),
              "Type" = ~ if(is.numeric(df$age_years)==TRUE) {print("Numeric")} else {print("Character")},
              "Min"       = ~ min(age_years),
              "Max"       = ~ max(age_years),
              "Mean" = ~ format(round(mean(df$age_years), 2), nsmall = 2),
              "SD" = ~ format(round(sd(df$age_years), 2), nsmall = 2)),
       "Female" =
         list("Missing"  = ~ sum(is.na(df$sex_DV)),
              "Type" = ~ if(is.numeric(df$sex_DV)==TRUE) {print("Numeric")} else {print("Character")},
              "Min"       = ~ min(sex_DV),
              "Max"       = ~ max(sex_DV),
              "Mean" = ~ format(round(mean(df$sex_DV), 2), nsmall = 2),
              "SD" = ~ format(round(sd(df$sex_DV), 2), nsmall = 2))
       )

whole <- summary_table(df,summary1)
whole

欢迎任何帮助。谢谢!

编辑:具有可重现部分的数据(不是实际数据)

> df
      age_years       sex
    1          33          0
    2          11          1
    3          45          1
    4          67          0
    5          8           0
    6          99          0

【问题讨论】:

  • 您能否提供一个基础数据样本?你可以dput(df) 输出这个
  • @EmilyKothe 我放了一个简短版本的 df,只有两个变量。这就是我想在摘要中显示表格的方式
  • 标签列的信息存储在哪里? (即它们是否存储为这些变量的属性?)
  • @EmilyKothe 哦,那来自另一个数据集。这更像是一个例子。我现在正在尝试遵循该结构。我的数据集中没有标签列,因此可以避免该部分。我想基本遵循wide dataset格式

标签: r summary


【解决方案1】:

鉴于提供的示例数据,您可以实现以下目标。

df <- data.frame(
   age_years = c(33L, 11L, 45L, 67L, 8L, 99L),
         sex = c(0L, 1L, 1L, 0L, 0L, 0L)
)

names <- names(df)
missing_counts <- sapply(df, function(x) sum(is.na(x)))
classes <- sapply(df, function(x) class(x))
min <- sapply(df, function(x) min(x, na.rm = TRUE))
max <- sapply(df, function(x) max(x, na.rm = TRUE))
sd <- sapply(df, function(x) sd(x, na.rm = TRUE))
mean <- sapply(df, function(x) mean(x, na.rm = TRUE))

knitr::kable(as.data.frame(cbind(names, missing_counts, classes, min, max, mean, sd), row.names = FALSE))
names missing_counts classes min max sd mean
age_years 0 integer 8 99 34.8161839762296 43.8333333333333
sex 0 integer 0 1 0.516397779494322 0.333333333333333

旁注,将性别编码为 0 和 1,您可能希望此数据是一个因素而不是数字,并且您可能不想报告此统计数据的平均值/SD。

【讨论】:

  • 谢谢!是的,我同意你的性别部分。顺便说一句,有没有办法让 R 为此吐出乳胶表的代码?使用summary_table(df,summary1) 很容易做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多