Purpose
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.
Sample Data
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)
Question
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?
}