Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a named list without having to type each object's name twice [duplicate]

Tags:

list

r

In interactive use, I occasionally need to bundle a largish set of objects together in a list. To get a list in which the elements keep their original names, I am forced to write something like list(Object1=Object1, Object2=Object2, ..... , Object25=Object25).

Is there some straightforward way to place a set of named objects in a list, such that they 'keep' their names, without having to type nameXXX=nameXXX for each one?

cars <- mtcars[1:2,1:2]
vowels <- c("a","e","i","o","u")
consonants <- setdiff(letters, vowels)

## I'd like to get this result...
list(consonants=consonants, vowels=vowels, cars=cars)
## $consonants
##  [1] "b" "c" "d" "f" "g" "h" "j" "k" "l" "m" "n" "p" "q" "r" "s" "t" "v" "w" "x"
## [20] "y" "z"
##
## $vowels
## [1] "a" "e" "i" "o" "u"
##
## $cars
##               mpg cyl
## Mazda RX4      21   6
## Mazda RX4 Wag  21   6

## ... but by doing something more like
f(consonants, vowels, cars)
like image 838
Josh O'Brien Avatar asked Jun 10 '15 19:06

Josh O'Brien


People also ask

How do you name a listobject?

By default, each ListObject object name begins with the word "List", followed by a number (no spaces). If an attempt is made to set the Name property to a name already used by another ListObject object, a run-time error is thrown.

What is the difference between a list and named list?

A list can even contain matrices, data frames, or functions as its elements. The list can be created using list () function in R. Named list is also created with the same function by specifying the names of the elements to access them. Named list can also be created using names () function to specify the names of elements after defining the list.

How to create a list with unique names from a column?

Create Unique List from Column with VBA 1 Press Alt + F11 to show Microsoft Visual Basic for Applications window, and click Insert > Module, then copy and... 2 Press F5 key or Run button to enable the VBA code, and in the popping dialog, select the list with duplicates and... 3 Now, a list with unique names has been created. See More....

How to avoid duplicate-name errors when creating named objects?

Therefore, when creating named objects, use unique names and be sure to check function return values for duplicate-name errors. If you try to create an object using a name that is in use by an object of same type, the function succeeds, returning a handle to the existing object, and GetLastError returns ERROR_ALREADY_EXISTS.


2 Answers

You can get the same structure with

mget(c("vowels", "consonants", "cars"))

but you do have to do have to quote the variable names which isn't super sexy.

like image 97
MrFlick Avatar answered Sep 21 '22 15:09

MrFlick


Here's what I used most recently.

It would be nice, though, if there were something more succinct (or something built into base R or a decent package), so please feel free to add other/better answers.

LIST <- function(...) {
    nms <- sapply(as.list(substitute(list(...))), deparse)[-1]
    setNames(list(...), nms)
}

LIST(vowels, consonants, cars)
# $vowels
# [1] "a" "e" "i" "o" "u"
# 
# $consonants
#  [1] "b" "c" "d" "f" "g" "h" "j" "k" "l" "m" "n" "p" "q" "r" "s" "t" "v" "w" "x"
# [20] "y" "z"
# 
# $cars
#               mpg cyl
# Mazda RX4      21   6
# Mazda RX4 Wag  21   6
like image 26
Josh O'Brien Avatar answered Sep 18 '22 15:09

Josh O'Brien