gpt4 book ai didi

r - 在循环中使用glue和dplyr获取关卡名称

转载 作者:行者123 更新时间:2023-12-02 20:02:21 24 4
gpt4 key购买 nike

我正在尝试在循环中使用 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,"

最佳答案

我们可以使用paste0,因为它是矢量化的。

library(dplyr)

mtcars%>%
count(carb, vs) %>%
mutate(description = paste0("carb number: ",carb, " in: vs ", vs,
" with freq: ", n))


# carb vs n description
# <dbl> <dbl> <int> <chr>
#1 1 1 7 carb number: 1 in: vs 1 with freq: 7
#2 2 0 5 carb number: 2 in: vs 0 with freq: 5
#3 2 1 5 carb number: 2 in: vs 1 with freq: 5
#4 3 0 3 carb number: 3 in: vs 0 with freq: 3
#5 4 0 8 carb number: 4 in: vs 0 with freq: 8
#6 4 1 2 carb number: 4 in: vs 1 with freq: 2
#7 6 0 1 carb number: 6 in: vs 0 with freq: 1
#8 8 0 1 carb number: 8 in: vs 0 with freq: 1

要使用glue,我们需要使用purrr中的map版本

library(dplyr)
library(glue)
library(purrr)

mtcars%>%
count(carb, vs) %>%
mutate(description = pmap_chr(list(carb, vs, n), function(a, b, c)
glue("carb number: ",{a}, " in: vs ", {b}, " with freq: ", {c})))

编辑

如果我们有不同的列要计数,我们可以将变量转换为符号

var = c("vs", "am")
library(rlang)

map(var, function(x) mtcars%>%
count(carb, !!sym(x)) %>%
mutate(description = paste0("carb number: ",carb, " in: ",
x, " " , !!sym(x)," with freq: ", n)))

#[[1]]
# A tibble: 8 x 4
# carb vs n description
# <dbl> <dbl> <int> <chr>
#1 1 1 7 carb number: 1 in: vs 1 with freq: 7
#2 2 0 5 carb number: 2 in: vs 0 with freq: 5
#3 2 1 5 carb number: 2 in: vs 1 with freq: 5
#4 3 0 3 carb number: 3 in: vs 0 with freq: 3
#5 4 0 8 carb number: 4 in: vs 0 with freq: 8
#6 4 1 2 carb number: 4 in: vs 1 with freq: 2
#7 6 0 1 carb number: 6 in: vs 0 with freq: 1
#8 8 0 1 carb number: 8 in: vs 0 with freq: 1

#[[2]]
# A tibble: 9 x 4
# carb am n description
# <dbl> <dbl> <int> <chr>
#1 1 0 3 carb number: 1 in: am 0 with freq: 3
#2 1 1 4 carb number: 1 in: am 1 with freq: 4
#3 2 0 6 carb number: 2 in: am 0 with freq: 6
#4 2 1 4 carb number: 2 in: am 1 with freq: 4
#5 3 0 3 carb number: 3 in: am 0 with freq: 3
#6 4 0 7 carb number: 4 in: am 0 with freq: 7
#7 4 1 3 carb number: 4 in: am 1 with freq: 3
#8 6 1 1 carb number: 6 in: am 1 with freq: 1
#9 8 1 1 carb number: 8 in: am 1 with freq: 1

或者使用for循环

for (i in var) {
print(mtcars%>%
count(carb, !!sym(i)) %>%
mutate(description = paste0("carb number: ",carb, " in: ", i, " " ,
!!sym(i), " with freq: ", n)))
}

关于r - 在循环中使用glue和dplyr获取关卡名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com