为什么矩阵的一行是列表? [R [英] Why a row of my matrix is a list? R

查看:160
本文介绍了为什么矩阵的一行是列表? [R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了两行X列矩阵的列表,称为list_of_matrices_by2

I got a list of matrices X rows by 2 columns, called list_of_matrices_by2

list_of_matrices_by2[1:3]
[[1]]
[,1]   [,2]
[1,] "7204" "d" 
[2,] "7204" "a" 

[[2]]
[,1]   [,2]
[1,] "2032" "b" 
[2,] "2032" "e" 
[3,] "2032" "a" 

[[3]]
[,1]  [,2]
[1,] "802" "d" 
[2,] "802" "b"

我想将所有矩阵堆叠在all_pairs矩阵中,所以我做到了

I want to stack all my matrices in a all_pairs matrix, so I did this

all_pairs=do.call(rbind,list_of_matrices_by2)
all_pairs[1:10]
[,1]   [,2]
[1,] "7204" "d" 
[2,] "7204" "a" 
[3,] "2032" "b" 
[4,] "2032" "e" 
[5,] "2032" "a" 
[6,] "802"  "d" 
[7,] "802"  "b" 
[8,] "4674" "c" 
[9,] "4674" "a" 
[10,] "3886" "b"
class(all_pairs)
[1] "matrix"

由于某种原因,我需要此矩阵的行属于类矩阵.但这不是问题,因为矩阵行是R中的矩阵,对.但是不!

For some reason, I need the rows of this matrix to be of class matrix. But it shouldn't be a problem since rows of matrix are matrix in R, right. But no!

all_pairs[1,]
[[1]]
[1] "7204"

[[2]]
[1] "d

这是我的问题: 1)为什么呢?矩阵的一行怎么可能是列表? 2)您要做什么才能使它起作用,即矩阵的每一行都必须是矩阵?

So here are my questions : 1) Why this? How come a row of a matrix can possibly be a list? 2) What would you do to make it work, i.e. each row of my matrix has to be a matrix?

推荐答案

我确定您的list_of_matrices_by2类似于:

x <- list(matrix(list("7204","7204","d","a"), ncol = 2),
          matrix(list("2032","2032","2032","b","e","a"), ncol = 2),
          matrix(list("802","802","d","b"), ncol = 2))

#[[1]]
#     [,1]   [,2]
#[1,] "7204" "d" 
#[2,] "7204" "a" 

#[[2]]
#     [,1]   [,2]
#[1,] "2032" "b" 
#[2,] "2032" "e" 
#[3,] "2032" "a" 

#[[3]]
#     [,1]  [,2]
#[1,] "802" "d" 
#[2,] "802" "b" 

unlist(lapply(x, class))
# [1] "matrix" "matrix" "matrix"

unlist(lapply(x, mode))
# [1] "list" "list" "list"

因此,您确实有一个矩阵,但它不是列表矩阵,而不是数字矩阵.您可以照常执行rbind:

So you do have a matrix, but it is not a matrix of list instead of numeric. You can perform rbind as usual:

y <- do.call(rbind, x)
#     [,1]   [,2]
#[1,] "7204" "d" 
#[2,] "7204" "a" 
#[3,] "2032" "b" 
#[4,] "2032" "e" 
#[5,] "2032" "a" 
#[6,] "802"  "d" 
#[7,] "802"  "b" 

它具有类矩阵",但仍具有模式列表".这就是为什么当您提取第一行时会得到一个列表的原因:

It has class "matrix", but still with mode "list". That is why when you extract the first row you get a list:

y[1, ]
#[[1]]
#[1] "7204"

#[[2]]
#[1] "d"


我不知道您如何获得这些矩阵.如果您可以控制生成过程,那么最好以数字矩阵结尾.如果无法控制,则需要按如下所示手动进行转换:


I don't know how you obtain those matrices. If you can control the generation process, it would be good that you end up with matrices of numeric. If you can not control it, you need convert them manually as below:

x <- lapply(x, function (u) matrix(unlist(u), ncol = 2))
unlist(lapply(x, mode))
# [1] "character" "character" "character"

那你就可以做

do.call(rbind, x)


相关:

  1. 为什么此矩阵不是数字?
  2. 如何创建列表矩阵?
  1. Why is this matrix not numeric?
  2. How to create a matrix of lists?

这篇关于为什么矩阵的一行是列表? [R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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