Search code examples
rdictionarykeyvaluepair

Dictionaries and pairs


In R I was wondering if I could have a dictionary (in a sense like python) where I have a pair (i, j) as the key with a corresponding integer value. I have not seen a clean or intuitive way to construct this in R. A visual of my dictionary would be:

(1, 2) --> 1

(1, 3) --> 3

(1, 4) --> 4

(1, 5) --> 3

EDIT: The line of code to insert these key value pairs is in a loop with counters i and j. For example suppose I have:

for(i in 1: 5)
{  
  for(j in 2: 4)
  {
    maps[i][j] = which.min(someVector)
  }
}

How do I change maps[i][j] to get the functionality I am looking for?


Solution

  • You can do this with a list of vectors.

    maps <- lapply(vector('list',5), function(i) integer(0))
    maps[[1]][2] <- 1
    maps[[1]][3] <- 3
    maps[[1]][4] <- 4
    maps[[1]][5] <- 3
    

    That said, there's probably a better way to do what you're trying to do, but you haven't given us enough background.