创建具有向量(不是标量)元素的矩阵

发布于 2025-02-13 03:34:37 字数 140 浏览 2 评论 0原文

我想创建一个像矩阵一样具有向量的对象,以供其元素。喜欢:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2025-02-20 03:34:37

您可以使用矩阵列表。最初,您可以创建一个向量列表:

vec <- list(1:3, 4:10, 2:5, letters[1:3])

然后将其胁到矩阵:

mat1 <- matrix(vec, nrow = 2, ncol = 2)
#     [,1]      [,2]       
#[1,] integer,3 integer,4  
#[2,] integer,7 character,3

mat2 <- matrix(vec, nrow = 2, ncol = 2, byrow = TRUE)
#     [,1]      [,2]       
#[1,] integer,3 integer,7  
#[2,] integer,4 character,3

提取其元素,例如:

mat1[1, 1][[1]]

第一个[1,1]正在获取矩阵元素。但是,由于元素是列表,请使用额外的[[1]]访问实际向量。


如果您想在打印它时查看其内容,请将其胁到数据框架。例如,

dat1 <- data.frame(mat1)
#                    X1         X2
#1              1, 2, 3 2, 3, 4, 5
#2 4, 5, 6, 7, 8, 9, 10    a, b, c

您可以稍后更改列名,例如,

names(dat1) <- c("col1", "col2")
#                  col1       col2
#1              1, 2, 3 2, 3, 4, 5
#2 4, 5, 6, 7, 8, 9, 10    a, b, c

通过dat1 $ col1dat1 [[1]dat1 [[[“ col1”)访问其列。 ]]。在这种情况下,每列都是列表。

如果您的最终目标是数据框架而不是矩阵,则可以直接构建此数据框架,但必须使用i()>:

data.frame(col1 = I(list(1:3, 4:10)),
           col2 = I(list(2:5, letters[1:3])) )
#          col1       col2
#1      1, 2, 3 2, 3, 4, 5
#2 4, 5, 6,....    a, b, c

无论如何,我不喜欢这些数据结构。它有可能使进一步的操作繁琐。

You can use a matrix list. Initially you create a list of vectors:

vec <- list(1:3, 4:10, 2:5, letters[1:3])

then coerce it to a matrix:

mat1 <- matrix(vec, nrow = 2, ncol = 2)
#     [,1]      [,2]       
#[1,] integer,3 integer,4  
#[2,] integer,7 character,3

mat2 <- matrix(vec, nrow = 2, ncol = 2, byrow = TRUE)
#     [,1]      [,2]       
#[1,] integer,3 integer,7  
#[2,] integer,4 character,3

To extract its elements, use for example:

mat1[1, 1][[1]]

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,

dat1 <- data.frame(mat1)
#                    X1         X2
#1              1, 2, 3 2, 3, 4, 5
#2 4, 5, 6, 7, 8, 9, 10    a, b, c

You can change column names later, like,

names(dat1) <- c("col1", "col2")
#                  col1       col2
#1              1, 2, 3 2, 3, 4, 5
#2 4, 5, 6, 7, 8, 9, 10    a, b, c

Accessing its columns by dat1$col1, dat1[[1]] or dat1[["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():

data.frame(col1 = I(list(1:3, 4:10)),
           col2 = I(list(2:5, letters[1:3])) )
#          col1       col2
#1      1, 2, 3 2, 3, 4, 5
#2 4, 5, 6,....    a, b, c

Anyway, I don't like these data structures. It potentially make further operation cumbersome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文