Murrmel5
Murrmel5

Reputation: 107

Naming a list after a variable

My database needs a certain input on the format:

list(symbol1=list(param1='value1', ...), symbol2=list(param2='value2', ...), ...)

As I don't need to pass any params, and will only need to look at one symbol at the time my call to the data base will look like this:

symbolList <- list("INSTR_SPACE::ID123456SWEDEN"=list())

(this works)

But here comes my problem.. I want to pass the string "INSTR_SPACE::ID123456SWEDEN" by variable when i create the list.

i.e

var = "INSTR_SPACE::ID123456SWEDEN" 
symbolList <- list(var=list())

This does not give me the desired result as it just declares var to a empty list within a list. I have tried working with as.character and setNames but I cant get it to work...

What would be the R-way of doing this?

Thanks in advance!

Upvotes: 0

Views: 59

Answers (1)

Dhawal Kapil
Dhawal Kapil

Reputation: 2757

its not working because constructor of list list() works in the form of list(tag = value()) and it auto-coerces tag to string hence var is considered as a string when you call it inside the constructor of parent list itself.

Correct way of doing this would be

  1. create parent list first

    symbolList<-list()
    
  2. insert child elements dynamically and yes these elements can be a list too.

    var<-"XYZ"
    symbolList[[var]]<-list()
    
    > symbolList
    $`INSTR_SPACE::ID123456SWEDEN`
    list()
    

Upvotes: 2

Related Questions