I want to stack several dataframes using their names as strings.
df1 <- data.frame(name = "normal", x = rnorm(5))
df2 <- data.frame(name = "uniform", x = runif(5))
df3 <- data.frame(name = "binomial", x = rbinom(5, 2, 0.5))
mynames <- c("df1", "df2", "df3")
newdf <- some_function(mynames)
# Desired result
newdf <- do.call(rbind, list(df1, df2, df3))
How can this be done?
Thanks,
Naresh
See ?mget On Tue, May 19, 2026, 6:49 AM Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:> > I want to stack several dataframes using their names as strings. > > df1 <- data.frame(name = "normal", x = rnorm(5)) > df2 <- data.frame(name = "uniform", x = runif(5)) > df3 <- data.frame(name = "binomial", x = rbinom(5, 2, 0.5)) > mynames <- c("df1", "df2", "df3") > > newdf <- some_function(mynames) > > # Desired result > newdf <- do.call(rbind, list(df1, df2, df3)) > > How can this be done? > > Thanks, > Naresh > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
?s 11:48 de 19/05/2026, Naresh Gurbuxani escreveu:> > I want to stack several dataframes using their names as strings. > > df1 <- data.frame(name = "normal", x = rnorm(5)) > df2 <- data.frame(name = "uniform", x = runif(5)) > df3 <- data.frame(name = "binomial", x = rbinom(5, 2, 0.5)) > mynames <- c("df1", "df2", "df3") > > newdf <- some_function(mynames) > > # Desired result > newdf <- do.call(rbind, list(df1, df2, df3)) > > How can this be done? > > Thanks, > Naresh > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, Something like this? fun <- function(df_names, pattern, envir = .GlobalEnv) { if(missing(df_names)) { if(missing(pattern)) { obj_names <- ls(envir = envir) df_list <- Filter(\(x) is(x, "data.frame"), mget(obj_names, envir = envir)) } else { df_names <- ls(pattern = pattern, envir = envir) df_list <- mget(df_names, envir = envir) } } else df_list <- mget(df_names, envir = envir) res <- do.call(rbind, df_list) row.names(res) <- NULL res } df1 <- data.frame(name = "normal", x = rnorm(5)) df2 <- data.frame(name = "uniform", x = runif(5)) df3 <- data.frame(name = "binomial", x = rbinom(5, 2, 0.5)) mynames <- c("df1", "df2", "df3") fun() fun(mynames) fun(pattern = "df") Hope this helps, Rui Barradas
This goal is flawed and excessively complicated to accomplish... what if someone
does this:
x <- 5
mynames <- c("df1", "df2", "x")
then they try rbinding a scalar with the data frames.
Just re-frame your question to pull source data frames from a dedicated list of
possible distributions instead and it becomes simple to implement and easy to
re-use:
dists <- list()
dists [["normal"]] <- data.frame(name = "normal", x =
rnorm(5))
dists [["uniform"]] <- data.frame(name = "uniform", x =
runif(5))
dists [["binomial"]] <- data.frame(name = "binomial", x
= rbinom(5, 2, 0.5))
mynames <- c("normal", "binomial")
some_function <- function(pool, nms) {
do.call(rbind, pool[nms])
}
newdf <- some_function(dists, mynames)
newdf
On May 19, 2026 3:48:56 AM PDT, Naresh Gurbuxani <naresh_gurbuxani at
hotmail.com> wrote:>
>I want to stack several dataframes using their names as strings.
>
>df1 <- data.frame(name = "normal", x = rnorm(5))
>df2 <- data.frame(name = "uniform", x = runif(5))
>df3 <- data.frame(name = "binomial", x = rbinom(5, 2, 0.5))
>mynames <- c("df1", "df2", "df3")
>
>newdf <- some_function(mynames)
>
># Desired result
>newdf <- do.call(rbind, list(df1, df2, df3))
>
>How can this be done?
>
>Thanks,
>Naresh
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
https://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
Sent from my phone. Please excuse my brevity.
[[alternative HTML version deleted]]