简体   繁体   中英

way to call multiple variables from a list in R

I have various variables in a list, and what I would like to do is to call them in my code, by using the list. ie:

variables <- as.list(c("variable1", "variable2", "variable3",......,"variablen"))

What i would like to do is sth like:

variables[1] <- c("my text")

and have sth like:

variable1 <- c("my text")

Sounds like you are looking for the function assign . The variable name is the first argument, which you can provide programmatically. Something like this:

assign(variables[[1]], 'my text')
variable1

Output: 'my text'


However using assign is generally recommended against. A named list would likely work just as well or perhaps better for you:

newlist = c('my text', 'hello', 'world')
names(newlist) = c('variable1', 'variable2', 'variable3')
newlist[['variable1']]

Output: 'my text'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM