Dictionaries and pairs

前端 未结 5 1952
时光取名叫无心
时光取名叫无心 2021-01-17 06:53

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

相关标签:
5条回答
  • 2021-01-17 07:12

    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.

    0 讨论(0)
  • 2021-01-17 07:16

    Here's how you'd do this in a data.table, which will be fast and easily extendable:

    library(data.table)
    d = data.table(i = 1, j = 2:5, value = c(1,3,4,3), key = c("i", "j"))
    
    # access the i=1,j=3 value
    d[J(1, 3)][, value]
    
    # change that value
    d[J(1, 3), value := 12]
    
    # do some vector assignment (you should stop thinking loops, and start thinking vectors)
    d[, value := i * j]
    

    etc.

    0 讨论(0)
  • 2021-01-17 07:16

    R matrices allow you to do this. There are both sparse and dense version. I beleive the tm-package uses a variation on sparse matrices to form its implementation of dictionaries. This shows how to extract the [i,j] elements of matrix M where [i,j] is represented as a a two-column matrix.

    M<- matrix(1:20, 5, 5)
    ij <- cbind(sample(1:5), sample(1:5) )
    
    > ij
         [,1] [,2]
    [1,]    4    4
    [2,]    1    2
    [3,]    5    3
    [4,]    3    1
    [5,]    2    5
    > M[ij]
    [1] 19  6 15  3  2
    

    @Justin also points out that you could use lists which can be indexed by position:

     L <- list( as.list(letters[1:5] ), as.list( paste(2,letters[1:5] ) ) )
    
    > L[[1]][[2]]
    [1] "b"
    > L[[2]][[2]]
    [1] "2 b"
    
    0 讨论(0)
  • 2021-01-17 07:19

    You can just use a data.frame:

    a = data.frame(spam = c("alpha", "gamma", "beta"), 
                   shrub = c("primus","inter","parus"),
                   stringsAsFactors = FALSE)
    rownames(a) = c("John", "Eli","Seth")
    > a
          spam  shrub
    John alpha primus
    Eli  gamma  inter
    Seth  beta  parus
    
    >     a["John", "spam"]
    [1] "alpha"
    

    This handles the case with a 2d dictionary style object with named keys. The keys can also be integers, although they might have to be characters in stead of numeric's.

    0 讨论(0)
  • 2021-01-17 07:28

    Both named lists and environments provide a mapping, but between a character string (the name, which acts as a key) and an arbitrary R object (the value). If your i and j are simple (as they appear in your example to be, being integers), you can easily make a unique string out of the pair of them by concatenating them with some delimiter. This would make a valid name/key

    mykey <- function(i, j) {
      paste(i, j, sep="|")
    }
    
    maps <- list()
    for(i in 1: 5) {  
      for(j in 2: 4) {
        maps[mykey(i,j)] <- which.min(someVector)
      }
    }
    

    You can extract any value for a specific i and j with

    maps[[mykey(i,j)]]
    
    0 讨论(0)
提交回复
热议问题