我有一个循环,可以为一列创建多个交叉表,但不是按顺序打印它们,而是需要按列添加它们,就像我们使用cbind()
一样,因为行数是相同的(因为交叉表是df中一列对其他列)。
以下是现有问题中的数据示例:How to apply for loop function into crosstab making in R
test1 <- structure(list(weight = c(0.2158, 0.799, 0.611, 0.4969, 0.3469,
1.0107, 0.6946, 0.9415, 1.4008, 0.6192), Q2_1 = structure(c(4,
4, 2, 2, 3, 3, 3, 2, 3, 2), label = "How worried, if at all, are you about each of the following? - You or someone in your family will get sick with COVID-19", format.spss = "F40.0", display_width = 5L, labels = c(Skipped = -1,
`Very worried` = 1, `Somewhat worried` = 2, `Not too worried` = 3,
`Not at all worried` = 4), class = c("haven_labelled", "vctrs_vctr",
"double")), Q2_2 = structure(c(3, 4, 2, 4, 3, 3, 4, 2, 3, 4), label = "How worried, if at all, are you about each of the following? - You might experience serious side effects from the COVID-19 vaccine", format.spss = "F40.0", display_width = 5L, labels = c(Skipped = -1,
`Very worried` = 1, `Somewhat worried` = 2, `Not too worried` = 3,
`Not at all worried` = 4), class = c("haven_labelled", "vctrs_vctr",
"double")), group = c("E", "E", "E", "D", "E", "E", "D", "E",
"D", "E")), row.names = c(NA, -10L), class = "data.frame")
下面是按顺序输出多个交叉表的函数:
library(pollster)
library(rlang)
for (i in colnames(test1)[2:3]) {
table <- crosstab(
df = test1,
x = !!sym(i),
y = group,
weight = weight,
pct_type = "column")
print(table)
}
Q2_1 D E
<chr> <dbl> <dbl>
1 Somewhat worried 19.2 47.8
2 Not too worried 80.8 29.9
3 Not at all worried 0 22.3
4 n 2.59 4.54
# A tibble: 4 × 3
Q2_2 D E
<chr> <dbl> <dbl>
1 Somewhat worried 0 34.2
2 Not too worried 54.0 34.6
3 Not at all worried 46.0 31.2
4 n 2.59 4.54
我不想按顺序打印它们,而是想让它们像命令cbind()
那样,在一个表中按列彼此连接,这样,我将有一个表,其中有相同的行和所有列,这些行和列是从两个交叉表中组合出来的。
D E D E
<chr> <dbl> <dbl> <dbl> <dbl>
1 Somewhat worried 19.2 47.8 0 34.2
2 Not too worried 80.8 29.9 54.0 34.6
3 Not at all worried 0 22.3 46.0 31.2
4 n 2.59 4.54 2.59 4.54
1条答案
按热度按时间6kkfgxo01#
您期望的输出为不同的列使用相同的名称。这通常是一个坏主意,所以您可能应该调整列名。
因此,在您的情况下,您可以使用以下方法之一:
首先,我们将交叉表结果存储在
list
中,并为第一列分配一个常量列名。现在你可以用
得到
正如您所看到的,列名被更改为唯一的。您可以使用
.name_repair = "minimal"
参数跳过此操作。另一种选择是将
purrr
包与left_join
结合使用这将返回
您可以通过
left_join
的.suffix
参数调整列名。默认值为x
和y
,结果为D.x
和D.y
。