2

I want a named list but I want to get those names in a lazy evaluation way. Is this possible? If not, how close can you get?

> a <- 1:3
> b <- 4:6
> foo_anon <- list(a, b)
> foo_anon
[[1]]
[1] 1 2 3

[[2]]
[1] 4 5 6

> names(foo_anon) # I am sad
NULL
> foo_named <- list(a = a, b = b)
> foo_named
$a
[1] 1 2 3

$b
[1] 4 5 6

> names(foo_named) # I am sad because I do not scale
[1] "a" "b"
jennybryan
  • 2,606
  • 2
  • 18
  • 33
  • `mget(c("a","b"))` or `setNames(list(1:3,4:6),c("a","b"))` maybe? – thelatemail Jul 28 '15 at 01:30
  • OK yes agree this is a duplicate with http://stackoverflow.com/questions/30765511/constructing-a-named-list-without-having-to-type-each-objects-name-twice. The word "lazy" appears no where on that page, which is a shame, because then I would have found it. Also a shame there is no better solution :( – jennybryan Jul 28 '15 at 01:34
  • The questions will be linked now, so hopefully future searchers with the same keywords will hit both questions :-) – thelatemail Jul 28 '15 at 01:36
  • FWIW, my suggestion was going to be the same as [flodel's comment](http://stackoverflow.com/questions/16951080/can-list-objects-be-created-in-r-that-name-themselves-based-on-input-object-name#comment24478616_16951151). – Joshua Ulrich Jul 28 '15 at 01:38

0 Answers0