Reputation: 23807
When making a list of objects, is there a way of keeping the object symbols to use them as names in the list?
The below uses get
and re-assigns names afterwards, and I wondered if there is another approach.
a_1 <- 1
a_2 <- 2
a_3 <- 3
ls_a <- lapply(ls(pattern = "a_"), get)
names(ls_a) <- ls(pattern = "a_")
ls_a
#> $a_1
#> [1] 1
#>
#> $a_2
#> [1] 2
#>
#> $a_3
#> [1] 3
Upvotes: 1
Views: 49
Reputation: 389325
mget
has this as a default behavior.
mget(ls(pattern = "a_"))
#$a_1
#[1] 1
#$a_2
#[1] 2
#$a_3
#[1] 3
Upvotes: 4