Pregunta

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?

¿Fue útil?

Solución 3

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.

Otros consejos

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)]]

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.

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.

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"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top