创建具有向量(不是标量)元素的矩阵
我想创建一个像矩阵一样具有向量的对象,以供其元素。喜欢:
(1,2,3)(1,3,6)(2,4,1)
(0,7,8)(2,3,4)(5,2,1)
(9,0,8 )(8,4,6)(1,1,1)
我该怎么办?
I want to create an object like a matrix with vectors for its elements. like:
(1,2,3) (1,3,6) (2,4,1)
(0,7,8) (2,3,4) (5,2,1)
(9,0,8) (8,4,6) (1,1,1)
What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用矩阵列表。最初,您可以创建一个向量列表:
然后将其胁到矩阵:
提取其元素,例如:
第一个
[1,1]
正在获取矩阵元素。但是,由于元素是列表,请使用额外的[[1]]
访问实际向量。如果您想在打印它时查看其内容,请将其胁到数据框架。例如,
您可以稍后更改列名,例如,
通过
dat1 $ col1
,dat1 [[1]
或dat1 [[[“ col1”)访问其列。 ]]
。在这种情况下,每列都是列表。如果您的最终目标是数据框架而不是矩阵,则可以直接构建此数据框架,但必须使用
i()
>:无论如何,我不喜欢这些数据结构。它有可能使进一步的操作繁琐。
You can use a matrix list. Initially you create a list of vectors:
then coerce it to a matrix:
To extract its elements, use for example:
The first
[1, 1]
is getting matrix element. But since an element is a list, use an extra[[1]]
to access the actual vector.If you want to see its contents when printing it, then coerce it to a data frame instead. For example,
You can change column names later, like,
Accessing its columns by
dat1$col1
,dat1[[1]]
ordat1[["col1"]]
. In this case, each column is a list.If your end goal is a data frame not a matrix, you can construct this data frame directly, but has to protect each column with
I()
:Anyway, I don't like these data structures. It potentially make further operation cumbersome.