Can Lists Be Created That Name Themselves Based on Input Object Names

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.

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 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)

Naming Lists Using User Input

The correct way to accomplish what you want here is a dict of lists:

>>> lists = {}
>>> lists['homework'] = [40, 60, 70]
>>> lists['tests'] = [35, 99, 20]
>>> lists
{'tests': [35, 99, 20], 'homework': [40, 60, 70]}
>>>

When you can ask for input, the input function (raw_input in Python 2.x) returns a string, which you can make the key of the dictionary.

Use data frame names when creating a list of/from data frames

You need to name them when creating the list:

dataframes <- list(df1=df1, df2=df2, df3=df3)

names(dataframes)
#"df1" "df2" "df3"

How to name the elements of an unnamed list

Use dplyr::lst

setlist2 <- dplyr::lst(wsb_b6, wsb_id8)

Automatically add variable names to elements of a list

The key to this is to re-make the list function to stick on the names when you don't supply the names as well.

listN <- function(...){
anonList <- list(...)
names(anonList) <- as.character(substitute(list(...)))[-1]
anonList
}

With this, you make modelList as follows:

modelList <- listN(mod1, mod2, mod3, mod4, mod5, mod6)

With the names attached:

R> names(modelList)
[1] "mod1" "mod2" "mod3" "mod4" "mod5" "mod6"

A fuller solution is given here, which is robust to the use of a mixture of anonymous and named arguments to list.

listN2 <- function(...){
dots <- list(...)
inferred <- sapply(substitute(list(...)), function(x) deparse(x)[1])[-1]
if(is.null(names(inferred))){
names(dots) <- inferred
} else {
names(dots)[names(inferred) == ""] <- inferred[names(inferred) == ""]
}
dots
}

R list from variables using variable names

A couple of ways I can think of include mget (make assumptions about the environment your objects are located in):

mget( c("a","b","c") )
$a
[1] 2

$b
[1] "foo"

$c
[1] 1 2 3 4

Or as an edit to your function, you could use, match.call like this:

named.list <- function(...) { 
l <- list(...)
names(l) <- as.character( match.call()[-1] )
l
}
named.list( a,b,c)
$a
[1] 2

$b
[1] "foo"

$c
[1] 1 2 3 4

Or you can do it in one go using setNames like this:

named.list <- function(...) { 
l <- setNames( list(...) , as.character( match.call()[-1]) )
l
}

named.list( a,b,c)
$a
[1] 2

$b
[1] "foo"

$c
[1] 1 2 3 4


Related Topics



Leave a reply



Submit