Reputation: 381
I would like to take ... (three dots) argument for grouping variables in dplyr and use ... as names of a new data frame in a function. Question section includes details about what I want to achieve.
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?
}
Upvotes: 1
Views: 471
Reputation: 887213
After we create the grouping attributes, get the group column names directly with group_vars
, then create the dataset dynamically using those names
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')))
}
-testing
> 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
Upvotes: 1