r-正在获取..(三个点)用于在dplyr中对变量进行分组的参数,并使用..作为函数中新数据帧的名称



目的

我想带。。。(三个点(在dplyr中对变量进行分组的参数,并使用。。。作为函数中新数据帧的名称。问题部分包括我想要实现的目标的详细信息。

样本数据

library(tidyverse)
library(tibble)
library(data.table)

rename <- dplyr::rename
select <- dplyr::select
set.seed(10002)
id <- sample(1:20, 1000, replace=T)

set.seed(10003)
group1 <- sample(letters, 1000, replace=T)

set.seed(10004)
group2 <- sample(LETTERS, 1000, replace=T)

df <-
data.frame(id, group1, group2) 

问题

fn <- function(df, ...){

group_ <- enquos(...)

# First I will use this as grouping variables in dplyr
df %>%
group_by(!!!group_) %>% 
summarise(obs = n()) 
# The question is the second operation.     
# I would like to create a data frame with NAs here so that I can rbind using for loop later
# for example, if ... = group1 
# f <- data.frame(id = NA, group1 = NA, output = NA)
# for example, if ... = group1, group2
# f <- data.frame(id = NA, group1 = NA, group1 = NA, output = NA)
# Is there a way to take the ... argument abd use them as column names in a new data frame f in a function? 


}

创建分组属性后,直接使用group_vars获取组列名,然后使用这些名称动态创建数据集

fn <- function(df, ...){

group_ <- enquos(...)


tmp <-   df %>%
group_by(!!!group_) %>% 
summarise(obs = n(), .groups = 'keep')
nm1 <- group_vars(tmp) 
tibble::as_tibble(setNames(rep(list(NA), length(nm1) + 2),
c('id', nm1, 'output')))
}  

-测试

> fn(df, group1)
# A tibble: 1 x 3
id    group1 output
<lgl> <lgl>  <lgl> 
1 NA    NA     NA    
> fn(df, group1, group2)
# A tibble: 1 x 4
id    group1 group2 output
<lgl> <lgl>  <lgl>  <lgl> 
1 NA    NA     NA     NA    

最新更新