I generated many variables as part of a project, and I am finding that I will be needing to re-combine them in a list (ex: list(var1, var2, ..., varn
). Is there a way to plug them in without typing up each one?
CodePudding user response:
You can always generate a list from an enviroment (list2env) and the inverse too (as.list):
L <- list(a = 1, b = 2:4, p = pi, ff = gl(3, 4, labels = LETTERS[1:3]))
e <- list2env(L)
e$ff
# [1] A A A A B B B B C C C C
#Levels: A B C
as.list(e)
#$ff
# [1] A A A A B B B B C C C C
#Levels: A B C
#
#$p
#[1] 3.141593
#
#$b
CodePudding user response:
What @Roland mentioned -
var1 <- 1:5
var2 <- 1:10
data <- mget(paste0('var', 1:2))
data
#$var1
#[1] 1 2 3 4 5
#$var2
# [1] 1 2 3 4 5 6 7 8 9 10
Also, with ls
and pattern
-
data <- mget(ls(pattern = 'var'))
CodePudding user response:
a <- 1
b <- '2'
(all_variables <- eapply(.GlobalEnv, function (x) x))
#> $a
#> [1] 1
#>
#> $b
#> [1] "2"
Created on 2021-12-25 by the reprex package (v2.0.1)