Get level names using glue and dplyr in a loop

匆匆过客 提交于 2020-07-07 07:10:16

问题


I am trying to get level names from a table using dplyr and glue in a loop (I use a loop because I get a large number of variable to get grouped tables and individual tables), I show an example below:

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

my result:

Groups: carb [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,"

My problem its that I can not get the level name from vs and am variables in this example. My goal is get individual tables grouped by carb:

[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,"

回答1:


We could use paste0 since it is vectorised.

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

To use glue we need to use some version of map from 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})))

EDIT

If we have different columns to count we can convert variables to symbol

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

Or with for loop

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



回答2:


We can use glue_data directly on the column without any looping

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

If we have different grouping variables, then use sym in count to convert to symbol and evaluate (!!), and change to .x in the glue_datafor the 'var' part

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


来源:https://stackoverflow.com/questions/55565462/get-level-names-using-glue-and-dplyr-in-a-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!