使用均值和标准差在 R 中生成交叉 table

Generate Cross-table in R with mean and SD

我有一个大型数据集,我需要为其生成多个交叉 table。这些特别是二维 tables,用于生成频率以及均值和 SD。

所以举个例子我有以下数据-

City <- c("A","B","A","A","B","C","D","A","D","C")
Q1 <- c("Agree","Agree","Agree","Agree","Agree","Neither","Neither","Disagree","Agree","Agree")
df <- data.frame(City,Q1)

记住数据,我想生成一个交叉table,其平均值如下 -

    City            
        A   B   C   D
Agree   3   2   1   1
Neither         1   1
Disagree    1           
Total   4   2   2   2
Mean    2.5 3   2.5 2.5

生成平均值时,同意的权重为 3,两者的权重均为 2,不同意的权重为 1。交叉 table 输出的平均值应略低于总计列。最好每列和每行之间有网格线。

能否请您建议如何在 R 中实现此目的?

这是一个解决方案:

x <- table(df$Q1, df$City) #building basic crosstab
#assigning weights to vector
weights <- c("Agree" = 3, "Disagree" = 1, "Neither" = 2)
#getting weighted mean
weightedmean <- apply(x, 2, function(x) {sum(x * weights)/sum(x)})
#building out table
x <- rbind(x,
           apply(x, 2, sum), #row sums
           weightedmean)
rownames(x)[4:5] <- c("Total", "Mean")

这是一个使用 addmargins 的可能解决方案,它允许您将预定义函数传递给 table 结果

wm <- function(x) sum(x * c(3, 1, 2)) / sum(x)
addmargins(table(df[2:1]), 1, list(list(Total = sum, Mean = wm)))

#           City
# Q1           A   B   C   D
#   Agree    3.0 2.0 1.0 1.0
#   Disagree 1.0 0.0 0.0 0.0
#   Neither  0.0 0.0 1.0 1.0
#   Total    4.0 2.0 2.0 2.0
#   Mean     2.5 3.0 2.5 2.5

如果您想要 SD,只需将 , SD = sd 添加到函数列表中即可