Home > Mobile >  Is it possible to make a character and return what that character's value in the the global env
Is it possible to make a character and return what that character's value in the the global env

Time:04-29

I am not sure if this is possible but I would like to return the global environment value of each element in a vector that was generated using sprintf.

v1 <- 'value 1'
v2 <- 'value 2'
v3 <- 'value 3'

obs <- noquote(sprintf('%s%d', 'v', 1:3))
obs

obs returns:

[1] v1 v2 v3

But I would like it to return the value of c(v1, v2, v3):

[1] "value 1" "value 2" "value 3"

CodePudding user response:

mget(obs)
# $v1
# [1] "value 1"
# $v2
# [1] "value 2"
# $v3
# [1] "value 3"
unlist(mget(obs))
#        v1        v2        v3 
# "value 1" "value 2" "value 3" 
  •  Tags:  
  • r
  • Related