在循环中使用胶水和dplyr获取级别名称

时间:2019-04-08 02:21:45

标签: r dplyr glue

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

请,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

我们可以使用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,我们需要使用map中某个版本的purrr

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)))
}

答案 1 :(得分:1)

我们可以直接在列上使用glue_data,而无需任何循环

library(glue)
library(dplyr)
mtcars %>% 
 count(carb, vs) %>%
 mutate(description = glue_data(., "carb number: {carb} in: vs {vs} with freq: {n}"))
# A tibble: 8 x 4
#   carb    vs     n description                         
#  <dbl> <dbl> <int> <S3: glue>                          
#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

如果我们有不同的分组变量,请使用sym中的count转换为symbol并求值(!!),然后更改为.x glue_data代表“ var”部分

library(rlang)
library(purrr)
map(var, ~ mtcars %>%
             count(carb, !! sym(.x)) %>%
              mutate(description = glue_data(., 
                  "carb number: {carb} in: vs {.x} with freq: {n}")))