Make a List of Many Objects from a Vector of Object Names

Make a list of many objects from a vector of object names

You can use mget to get objects from the environment and put them in a list.

mget(VECTOR_OF_NAMES_OF_OBJECTS)

You can also combine mget with ls() so that you don't have to type the object names your self.

obj1 = 1:2
obj2 = 3:7
obj13 = 8:11

mget(ls(pattern = "obj\\d+"))
#$obj1
#[1] 1 2

#$obj13
#[1] 8 9 10 11

#$obj2
#[1] 3 4 5 6 7

Create new lists inside of a list based on object names

An option would be split using the extracted substring of 'B'

split(v1, gsub("A\\d+_|\\.C\\d+", "", v1))
#$B1
#[1] "A1_B1.C1" "A2_B1.C1" "A3_B1.C1"

#$B2
#[1] "A1_B2.C2" "A2_B2.C2" "A3_B2.C2"

#$B3
#[1] "A1_B3.C3" "A2_B3.C3" "A3_B3.C3"

NOTE: It is not clear whether these are object identifiers or not

The OP uses a different string pattern

split(v2, gsub("^[^_]+\\_|\\..*$", "", v2))

data

v1 <- c("A1_B1.C1", "A1_B2.C2", "A1_B3.C3", "A2_B1.C1", "A2_B2.C2", 
"A2_B3.C3", "A3_B1.C1", "A3_B2.C2", "A3_B3.C3")

v2 <- "GenetypeA_Drug1.ValueA"

Create a list based on a character vector of names of objects

We can use mget to return the values of the object names

mget(paste0('l', 1:2))

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)

assign names to each object inside a list using a vector r

You could do the following:

mylist <- list(TRUE, LETTERS[1:3],  1:5)
names(mylist) <- c("a", "b", "c")

mylist

Returns:

$a
[1] TRUE

$b
[1] "A" "B" "C"

$c
[1] 1 2 3 4 5

Or to use the vector a you mention, it's the same idea:

a <- c("a", "b", "c")
names(mylist) <- a

R: transforming vector of objects in a vector of object names

garch11fit=1
egarchfit=2
models <- c(garch11fit=garch11fit, egarchfit=egarchfit)
names(models)

If you name the variables, you can get their names back. But in your code, the names aren't associated in the models variable, only the values of garch11fit and egarchfit, so you can't get the names back.

name objects from vector values

1) assign Normally one would want to assign the values into a list but if you must assign them into the global environment then this is a good application for a for loop:

for(nm in Named) assign(nm, matrix(0L, 3, 3), .GlobalEnv)

If you are already doing this at the global environment level then you could optionally omit the last argument to assign.

2) index global environment This would also work:

for(nm in Named) .GlobalEnv[[nm]] <- matrix(0L, 3, 3)

3) list2env Another approach is to create a list then use list2env to copy its components to the global environment.

list2env( Map(function(x) matrix(0L, 3, 3), Named), .GlobalEnv )

4) hard coding Note that if you just have a few elements to assign and you don't need to parameterize by a variable such as Named you could just hard code it like this:

a <- b <- c <- matrix(0L, 3, 3)

5) list If in the end you do decide to create a list instead of objects in the global environment then:

L <- Map(function(x) matrix(0L, 3, 3), Named)


Related Topics



Leave a reply



Submit