首页 > 解决方案 > 调用具有可变数量参数的函数

问题描述

library(plyr)

stack_dataframes <- function(...){
  
  dataframes <- list(...)
  
  for (i in dataframes){
  colnames(i) <- "b"
  }
  
  plyr::rbind.fill(...)  
  # Still has the colnames "a", but I want to stack 
  # all the changed dataframes with colname "b"
}

一些样本数据和三个参数调用函数

df1 <- data.frame(a=c(1,2))
df2 <- data.frame(a=c(1,2))
df3 <- data.frame(a=c(1,2))

stack_dataframes(df1, df2, df3)

但是如何更改stack_dataframes()-function 以便它plyr::rbind.fill()根据 data.frames 的数量自动调整参数?例如,如果我stack_dataframes(df1, df2)用两个 data.frames 调用它也应该是plyr::rbind.fill(df1, df2)等。

标签: rtidyverse

解决方案


推荐阅读