Extract Names of Dataframes Passed with Dots

Extract names of dataframes passed with dots

You can try the following:

names_from_dots <- function(...) sapply(substitute(list(...))[-1], deparse)

names_from_dots(swiss, iris)
# [1] "swiss" "iris"

How to get names of dot-dot-dot arguments in R

You can use deparse and substitute to get what you want (see also this Q&A):

test<-function(x, y, ...)
{
varnames=lapply(substitute(list(...))[-1], deparse)
lapply(varnames, print)
return(invisible())
}

test(12,12,v1,v2)
#[1] "v1"
#[1] "v2"

Getting names from ... (dots)

Using the guidance here How to use R's ellipsis feature when writing your own function?

eg substitute(list(...))

and combining with with as.character

rbind.test <- function(...) {
.x <- as.list(substitute(list(...)))[-1]
as.character(.x)
}

you can also use

rbind.test <- function(...){as.character(match.call(expand.dots = F)$...)}

how to fetch/extract the names of dataframes from a list of dataframes in a for-loop to use it inside the same for-loop?

Why not put the datafarmes into a dictionary and use the key to name the csv:

dict_containing_dataframes = {'Furnishings': Furnishings, 'Labels': Labels, 'Accessories': Accessories, 'Binders': Binders, 'Supplies': Supplies, 'Phones': Phones, 'Tables': Tables}

for key, one_dataframe in dict_containing_dataframes.items()
newly_generated_dataframes.to_csv(key+'_forecast' , index = False, encoding = 'utf-8')

Extract colnames from a nested list of data.frames

There are already a couple of answers. But let me leave another approach. I used rapply2() in the rawr package.

devtools::install_github('raredd/rawr')
library(rawr)
library(purrr)

rapply2(l = l, FUN = colnames) %>%
flatten

$a
[1] "a" "b" "c"

$b
[1] "a" "b" "c"

$c
[1] "a" "b" "c"

data frame from a list of vectors

You can define a function that takes any number of vectors:

build_df <- function(...)
{
vec_list <- list(...)
df <- data.frame(x = do.call("c", sapply(vec_list, seq_along)),
y = do.call("c", vec_list),
name = do.call("c", sapply(seq_along(vec_list),
function(i) rep(names(vec_list)[i],
length(vec_list[[i]]))))
)
rownames(df) <- seq(nrow(df))
df
}

build_df(d1 = 1:3, d2 = 6:9, bananas = 4:6)
#> x y name
#> 1 1 1 d1
#> 2 2 2 d1
#> 3 3 3 d1
#> 4 1 6 d2
#> 5 2 7 d2
#> 6 3 8 d2
#> 7 4 9 d2
#> 8 1 4 bananas
#> 9 2 5 bananas
#> 10 3 6 bananas

Created on 2020-08-03 by the reprex package (v0.3.0)



Related Topics



Leave a reply



Submit