英文:
Use variables from tidy select within paste()
问题
以下是翻译好的代码部分:
假设以下函数
library(tidyverse)
nest_fun <- function(data, groups) {
data %>%
group_by(pick({{groups}})) %>%
nest() %>%
mutate(filename = paste({{groups}}[1], {{groups}}[2], "file.png", sep = "_"))
}
nest_fun(data = mtcars, groups = c(vs, am))
# # A tibble: 4 × 4
# # Groups: vs, am [4]
# vs am data filename
# <dbl> <dbl> <list> <chr>
# 1 0 1 <tibble [6 × 9]> 0_1_file.png
# 2 1 1 <tibble [7 × 9]> 1_1_file.png
# 3 1 0 <tibble [7 × 9]> 1_0_file.png
# 4 0 0 <tibble [12 × 9]> 0_0_file.png
这个函数在只按照2个变量分组时运行良好。如何修改函数以便可以根据任意数量的变量分组并仍然获得正确的文件名?
英文:
Assume the following function
library(tidyverse)
nest_fun <- function(data, groups) {
data %>%
group_by(pick({{groups}})) %>%
nest() %>%
mutate(filename = paste({{groups}}[1], {{groups}}[2], "file.png", sep = "_"))
}
nest_fun(data = mtcars, groups = c(vs, am))
# # A tibble: 4 × 4
# # Groups: vs, am [4]
# vs am data filename
# <dbl> <dbl> <list> <chr>
# 1 0 1 <tibble [6 × 9]> 0_1_file.png
# 2 1 1 <tibble [7 × 9]> 1_1_file.png
# 3 1 0 <tibble [7 × 9]> 1_0_file.png
# 4 0 0 <tibble [12 × 9]> 0_0_file.png
The function works fine as long as I group by exactly 2 variables. How could I write the function such that I can group by an arbitrary number of variables and still get filename correct?
答案1
得分: 1
paste
(和paste0
)的collapse
参数将向量的元素粘贴在一起,而sep
则在函数调用的参数之间粘贴。因此,您可以通过嵌套调用paste
/paste0
来实现想要的效果。
我强烈支持@r2evans对将数据框作为函数参数的评论。这是一个良好的实践(也允许您使用管道函数)。我看到您在问题中已经采取了这种做法。然而,将其命名为data
- 这是一个内置函数的名称 - 并不理想。
英文:
The collapse
argument of paste
(and paste0
) pastes down the elements of a vector, whereas sep
pastes across the arguments of the call. Therefore you can get what you want with nested calls to paste
/paste0
.
I strongly support @r2evans' comment about providing the data frame as an argument to your function. It is simply good practice (and allows you to pipe your function if you wish). I see you've already actioned that in the question. However, using data
- which is the name of a built-in function - as its name is not ideal.
So...
library(tidyverse)
nest_fun <- function(d=mtcars, groups = c(vs, am)) {
d %>%
group_by(pick({{groups}})) %>%
nest() %>%
mutate(filename = paste0(paste({{groups}}, collapse="_"), "_file.png"))
}
nest_fun()
# A tibble: 4 × 4
# Groups: vs, am [4]
vs am data filename
<dbl> <dbl> <list> <chr>
1 0 1 <tibble [6 × 9]> 0_1_file.png
2 1 1 <tibble [7 × 9]> 1_1_file.png
3 1 0 <tibble [7 × 9]> 1_0_file.png
4 0 0 <tibble [12 × 9]> 0_0_file.png
mtcars %>% nest_fun(c(vs, am, gear))
# A tibble: 7 × 5
# Groups: vs, am, gear [7]
vs am gear data filename
<dbl> <dbl> <dbl> <list> <chr>
1 0 1 4 <tibble [2 × 8]> 0_1_4_file.png
2 1 1 4 <tibble [6 × 8]> 1_1_4_file.png
3 1 0 3 <tibble [3 × 8]> 1_0_3_file.png
4 0 0 3 <tibble [12 × 8]> 0_0_3_file.png
5 1 0 4 <tibble [4 × 8]> 1_0_4_file.png
6 0 1 5 <tibble [4 × 8]> 0_1_5_file.png
7 1 1 5 <tibble [1 × 8]> 1_1_5_file.png
nest_fun()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论