0

I am trying to store a list within a list in a way that I can access it by list ID just like Python dictionary:

idList <- list('A','B','C','D','E')

subnet <- list('A'=0,'B'=0,'C'=0,'D'=0,'E'=0)


for (ids in idList) {
 
  tmp <- c('some','random','list')

 subnet$ids <- tmp

}

But when I check the results as subnet$A I get the value 0

I am not able to understand why the tmp value is not being stored in the subnet list?

When I do it on the command line individually, then it works:

   R> subnet$A <- tmp
   R> subnet$A
   R> "some" "random" "list"

To me R is weird and it works in a completely unintuitive way.

Ashutosh
  • 425
  • 1
  • 5
  • 18
  • 1
    Use `subnet[[ids]] <- tmp` – Ronak Shah Jan 31 '21 at 01:41
  • 1
    I think you want `subnet[[ids]]` instead of `subnet$ids`. The `list$var` syntax is literally using the name `var` to access the list element. It is not evaluating `var` to a character. The `list[[var]]` will evaluate `var` to the character. – Cole Jan 31 '21 at 01:41
  • But can you explain why it works when I try to run it individually on R- console ? I mean the same ```subnet$ids <- tmp``` assigns value to it when run on console – Ashutosh Jan 31 '21 at 02:04

0 Answers0