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
Usage of Dot/Period in R Functions
Sum Columns by Group (Row Names) in a Matrix
Combining .Sd with Renamed Variable Messes with Names of .Sd Columns
Increasing Whitespace Between Legend Items in Ggplot2
"Dims [Product Xx] Do Not Match the Length of Object [Xx]" Error in Using R Function 'Outer'
Why Does "Hello" > 0 Return True
Retain Numerical Precision in an R Data Frame
Creating a Prng Engine for <Random> in C++11 That Matches Prng Results in R
Shiny Ui.R - Error in Tag("Div", List(...)) - Not Sure Where Error Is
Dummy Variables to Single Categorical Variable (Factor) in R
R:Function to Generate a Mixture Distribution
R: Split String into Numeric and Return the Mean as a New Column in a Data Frame
Non-Equi-Joins in R with Data.Table - Backticked Column Name Trouble
Why Isn't the R Function Sink() Writing a Summary Output to My Results File