我正在尝试使用 dplyr 从表中获取级别名称并在循环中粘合(我使用循环是因为我获得了大量变量来获取分组表和单个表),我在下面展示了一个示例:
library(dplyr)
library(glue)
var=c( "vs", "am")
for(i in var) {
bd=mtcars%>%
group_by(carb) %>%
count_(i) %>%
mutate(descripcion = glue("carb number:{carb} in: {i} with freq: {n},"))
print(bd)
print(bd$descripcion)
}
我的结果:
组:碳水化合物 [6]
carb vs n descripcion
<dbl> <dbl> <int> <chr>
1 1 1 7 carb number:1 in: vs with freq: 7,
2 2 0 5 carb number:2 in: vs with freq: 5,
3 2 1 5 carb number:2 in: vs with freq: 5,
4 3 0 3 carb number:3 in: vs with freq: 3,
5 4 0 8 carb number:4 in: vs with freq: 8,
6 4 1 2 carb number:4 in: vs with freq: 2,
7 6 0 1 carb number:6 in: vs with freq: 1,
8 8 0 1 carb number:8 in: vs with freq: 1,
[1] "carb number:1 in: vs with freq: 7," "carb number:2 in: vs with freq: 5,"
[3] "carb number:2 in: vs with freq: 5," "carb number:3 in: vs with freq: 3,"
[5] "carb number:4 in: vs with freq: 8," "carb number:4 in: vs with freq: 2,"
[7] "carb number:6 in: vs with freq: 1," "carb number:8 in: vs with freq: 1,"
# A tibble: 9 x 4
# Groups: carb [6]
carb am n descripcion
<dbl> <dbl> <int> <chr>
1 1 0 3 carb number:1 in: am with freq: 3,
2 1 1 4 carb number:1 in: am with freq: 4,
3 2 0 6 carb number:2 in: am with freq: 6,
4 2 1 4 carb number:2 in: am with freq: 4,
5 3 0 3 carb number:3 in: am with freq: 3,
6 4 0 7 carb number:4 in: am with freq: 7,
7 4 1 3 carb number:4 in: am with freq: 3,
8 6 1 1 carb number:6 in: am with freq: 1,
9 8 1 1 carb number:8 in: am with freq: 1,
[1] "carb number:1 in: am with freq: 3," "carb number:1 in: am with freq: 4,"
[3] "carb number:2 in: am with freq: 6," "carb number:2 in: am with freq: 4,"
[5] "carb number:3 in: am with freq: 3," "carb number:4 in: am with freq: 7,"
[7] "carb number:4 in: am with freq: 3," "carb number:6 in: am with freq: 1,"
[9] "carb number:8 in: am with freq: 1,"
我的问题是在此示例中我无法从 vs 和 am 变量中获取级别名称。我的目标是让各个表格按碳水化合物分组:
[1] "carb number:1 in: vs 1 with freq: 7," "carb number:2 in: vs 0 with freq: 5,"
[3] "carb number:2 in: vs 1 with freq: 5," "carb number:3 in: vs 0 with freq: 3,"
[5] "carb number:4 in: vs 0 with freq: 8," "carb number:4 in: vs 1 with freq: 2,"
[7] "carb number:6 in: vs 0 with freq: 1," "carb number:8 in: vs 0 with freq: 1,"