Use object names as list names in R
Find the names, then call mget
.
If there is a pattern to the names of each individual variable, then this is straightforward.
var_names <- paste0("df", 1:3)
mget(var_names, envir = globalenv()) #or maybe envir = parent.frame()
If the naming system is more complicated, you can use regular expressions to find them, using something like
var_names <- ls(envir = globalenv(), pattern = "^df[[:digit:]]+$")
How to name a list object in R
If you want to name the lists while creating them you can use tibble::lst
my_df1 <- mtcars[1:10,]
my_df2 <- mtcars[11:20,]
my_df3 <- mtcars[21:30,]
my_list <- tibble::lst(my_df1, my_df2, my_df3)
If the list is already created you can use names
or setNames
to name the list.
names(my_list) <- c('list1', 'list2', 'list3')
setNames(my_list, c('list1', 'list2', 'list3'))
Naming list elements in R
The first case is the correct usage. In the second case you are sending filList[i]
to names<-
which is only exists as a temporary subsetted object.
Alternatively, you could just do everything outside the loop with:
names(filList) <- names(Fil)
Can lists be created that name themselves based on input object names?
Coincidentally, I just wrote this function. It looks a lot like @joran's solution, but it tries not to stomp on already-named arguments.
namedList <- function(...) {
L <- list(...)
snm <- sapply(substitute(list(...)),deparse)[-1]
if (is.null(nm <- names(L))) nm <- snm
if (any(nonames <- nm=="")) nm[nonames] <- snm[nonames]
setNames(L,nm)
}
## TESTING:
a <- b <- c <- 1
namedList(a,b,c)
namedList(a,b,d=c)
namedList(e=a,f=b,d=c)
Copied from comments: if you want something from a CRAN package, you can use Hmisc::llist
:
Hmisc::llist(a, b, c, d=a, labels = FALSE)
The only apparent difference is that the individual vectors also have names in this case.
Give name to list variable
Since @akrun doesn't need any more points, here is an example showing how you can assign names to a list:
lst <- list(a="one", b="two", c=c(1:3))
names(lst)
[1] "a" "b" "c"
names(lst) <- c("x", "y", "z")
> lst
$x
[1] "one"
$y
[1] "two"
$z
[1] 1 2 3
How can I make a named list or vector from environment objects, using the object names?
You can use match.call
to retrieve the names of the arguments being passed in
name_list <- function(...){
vnames <- as.character(match.call())[-1]
return(setNames(list(...), vnames))
}
a = 1; b=c(1,2)
name_list(a, b)
Get the name of the list object then add that name as a new column of the each list
If I've understood you correctly, I'd probably use the purrr
library. imap_dfr
will pass both your matrix and its name to the function, and then bind the results into a single dataframe.
library(purrr)
new_list <- imap_dfr(mylist, function(mat, name) {
result <- as.data.frame(mat)
result$name <- name
result
})
gives
> new_list
Analyte_Mean SDBetweenGroup SDWithinGroup replicates NumSamples FValue PValue FCritical name
1 5.1210 0.008363944 0.01994994 5 10 1.8788386 0.0836504 2.124029 SiO2
2 2.0812 0.005310367 0.01590597 5 10 0.4426877 0.9032892 2.124029 Al2O3
Related Topics
Regression Line for the Entire Data Set Together with Regression Lines Based on Groups
Calculating Standard Deviation Across Rows
Usemethod("Predict"):No Applicable Method for 'Predict' Applied to an Object of Class "Train"
Combine Result from Top_N with an "Other" Category in Dplyr
Ggsave Png Error with Larger Size
Edit Individual Ggplots in Ggally::Ggpairs: How to Have the Density Plot Not Filled in Ggpairs
How to Scale the Size of Line and Point Separately in Ggplot2
How to Plot X-Axis Labels and Bars Between Tick Marks in Ggplot2 Bar Plot
Changing the Appearance of Facet Labels Size
R: Row-Wise Dplyr::Mutate Using Function That Takes a Data Frame Row and Returns an Integer
Add Columns to a Reactive Data Frame in Shiny and Update Them
Convert R Dataframe from Long to Wide Format, But with Unequal Group Sizes, for Use with Qcc
Avoid Copying the Whole Vector When Replacing an Element (A[1] <- 2)
How to Write an Xts Object Using Write.CSV in R
Is There a Fast Parser for Date
Manually Colouring Plots with 'Scale_Fill_Manual' in Ggplot2 Not Working
How to Read the Files in a Directory in Sorted Order Using R