如何打印带有均值和标准误差的 table

How to print a table with mean and standard error

我正在尝试报告一个 table 完整的报告,其中包含我正在处理的数据集的描述性统计数据,包括均值、括号中的标准误差和分组变量。数据集如下(感谢dput函数,我在这里报告)。

> dput(dati)
structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27), 
    sex = structure(c(1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("F", "M"), class = "factor"), d8 = c(21, 
    21, NA, 23.5, 21.5, 20, 21.5, 23, NA, 16.5, 24.5, 26, 21.5, 
    23, 25.5, 20, 24.5, 22, 24, 23, 27.5, 23, 21.5, 17, 22.5, 
    23, 22), d10 = c(20, 21.5, 24, 24.5, 23, 21, 22.5, 23, 21, 
    19, 25, 25, 22.5, 22.5, 27.5, 23.5, 25.5, 22, 21.5, 20.5, 
    28, 23, 23.5, 24.5, 25.5, 24.5, 21.5), d12 = c(21.5, 24, 
    NA, 25, 22.5, 21, 23, 23.5, NA, 19, 28, 29, 23, NA, 26.5, 
    22.5, 27, 24.5, 24.5, 31, 31, 23.5, 24, 26, 25.5, 26, 23.5
    ), d14 = c(23, 25.5, 26, 26.5, 23.5, 22.5, 25, 24, 21.5, 
    19.5, 28, 31, 26.5, 27.5, 27, 26, 28.5, 26.5, 25.5, 26, 31.5, 
    25, 28, 29.5, 26, 30, 25)), row.names = c(NA, -27L), spec = structure(list(
    cols = list(id = structure(list(), class = c("collector_double", 
    "collector")), sex = structure(list(), class = c("collector_character", 
    "collector")), d8 = structure(list(), class = c("collector_double", 
    "collector")), d10 = structure(list(), class = c("collector_double", 
    "collector")), d12 = structure(list(), class = c("collector_double", 
    "collector")), d14 = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

我能够绘制从 Rcmdr 库自动生成的正确代码,尽管它返回一个 table 具有均值和标准差

#####Summary table of sample characteristics#####
CatTable <- CreateCatTable(vars = c("sex"), strata="sex", data=dati_locf)
matCatTable <- print(CatTable, printToggle = FALSE, showAllLevels = TRUE, 
  exact=c("sex"), smd = FALSE)
matCatTable <- matCatTable[,colnames(matCatTable)!="test"]
matCatTable <- cbind(Factor=row.names(matCatTable), matCatTable)
ContTable <- CreateContTable(vars = c("d8", "d10", "d12", "d14"), 
  strata="sex", data=dati_locf)
matContTable <- print(ContTable, printToggle = FALSE, explain = FALSE, smd 
  = FALSE)
matContTable <- matContTable[,colnames(matContTable)!="test"]
matContTable <- cbind(level="", matContTable)
matContTable <- cbind(Factor=row.names(matContTable), matContTable)
FinalTable <- as.matrix(matCatTable)
tempStrata <- attributes(FinalTable)[[2]][2]
FinalTable <- rbind(FinalTable, matContTable)
attributes(FinalTable) <- c(list(dim=attributes(FinalTable)[[1]]), 
  list(dimnames=c(attributes(FinalTable)[[2]][1], tempStrata)))
colnames(FinalTable)[2] <- "Group"
colnames(FinalTable)[colnames(FinalTable)=="p"] <- "p.value"
row0 <- colnames(FinalTable)
row1 <- FinalTable[1,]
row1 <- matrix(row1, nrow=1)
colnames(row1) <- row0
FinalTable <- FinalTable[which(rownames(FinalTable)!="n"),]
FinalTable <- rbind(n=row1, FinalTable)
FinalTable <- rbind(row0, FinalTable)
row0 <- rep("", length(colnames(FinalTable)))
row0[3] <- "sex"
FinalTable <- rbind(row0, FinalTable)
finaltable_dataframe_print(FinalTable)
write.table(FinalTable, "clipboard", sep = "    ", row.names = FALSE, 
  col.names=FALSE)

我怎样才能得到这样的项目,只是用标准误差代替标准差?

编写您自己的 se 函数,然后将其添加到 CreateContTable 参数中。这是一种方法:

se = function(x) sd(x)/sqrt(sum(!is.na(x)))

ContTable <- CreateContTable(
vars = c("d8", "d10", "d12", "d14")
,strata="sex"
,data=dati_locf
,funcAdditional = list(se=se)
)

summary(ContTable) #Shows you se is included in output