r中列出的数组 [英] array of lists in r

查看:125
本文介绍了r中列出的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,如果我想有10个元素的数组每个元素都是一个列表/地图。我这样做:

Suppose if I want to have 10 element array each element is a list/map. I am doing this:

x = array(list(), 10)
x[1][[ "a" ]] = 1
Warning message:
In x[1][["a"]] = 1 :
  number of items to replace is not a multiple of replacement length
>

这是正确的做法?我想阵列的每个元素是一个地图。

Is this the right approach? I want each element of the array to be a map.

推荐答案

你叫什么阵列通常只是叫R.名单你得到通过的区别绊倒了 [ [的名单。请参见递归(列表类似)对象帮助([)

What you're calling an "array" is usually just called a list in R. You're getting tripped up by the difference between [ and [[ for lists. See the section "Recursive (list-like) objects" in help("[").

x[[1]][["a"]] <- 1

更新:结果
请注意,上述解决方案,创建一个名为向量列表。换句话说,像

UPDATE:
Note that the solution above creates a list of named vectors. In other words, something like

x[[1]][["a"]] <- 1
x[[1]][["b"]] <- 1:2

将无法工作,因为你不能分配多个值向量的一个元素。如果你希望能够为矢量分配给一个名字,你可以用列表的列表。

won't work because you can't assign multiple values to one element of a vector. If you want to be able to assign a vector to a name, you can use a list of lists.

x[[1]] <- as.list(x[[1]])
x[[1]][["b"]] <- 1:2

这篇关于r中列出的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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