字典和对在R [英] Dictionaries and pairs in R

查看:213
本文介绍了字典和对在R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RI在想知道我是否可以有一个字典(在某种意义上像python),我有一对(i,j)作为具有相应整数的键值。我没有看到一个干净或直观的方式在R中构造。我的字典的视觉是:



(1,2) - > 1



(1,3) - > 3



(1,4) - > 4



(1,5) - > 3



感谢所有帮助!



编辑:插入这些键值对的代码行是与计数器i和j的循环。例如,假设我有:

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

如何更改 maps [i] [j] 以获取我正在寻找的功能?

解决方案

您可以使用向量列表来执行此操作。

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

可以说,可能有更好的方法来做你想做的事情,但是你没有给我们足够的背景。


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

Thanks for all the help!

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?

解决方案

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.

这篇关于字典和对在R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆