问题描述
在R中,我想知道我是否可以拥有一个字典(在某种意义上类似于python),其中我有一对(i, j)
作为具有对应整数值的键.我还没有在 R 中看到一种干净或直观的方法来构造它.我的字典的视觉效果是:
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
编辑:插入这些键值对的代码行与计数器i和j处于循环中.例如,假设我有:
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)
}
}
如何更改maps[i][j]
以获得所需的功能?
How do I change maps[i][j]
to get the functionality I am looking for?
推荐答案
您可以使用向量列表来完成此操作.
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.
这篇关于字典和对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!