热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么我的矩阵成为向量的向量?-Whydoesmymatrixbecomeavectorofvectors?

IhaveatablethatImreadingintoR(asadata.frame),andthenselectingsomenumericcolumnsou

I have a table that I'm reading into R (as a data.frame), and then selecting some numeric columns out of that data.frame to make into a matrix. I would expect to get a 2D matrix from this, however this is what I get:

我有一个表,我正在读R(作为data.frame),然后从该data.frame中选择一些数字列,使其成为一个矩阵。我期望得到一个2D矩阵,但这是我得到的:

full = read.table('complete_dataset.txt', header=T, sep="\t", quote="",row.names=NULL, stringsAsFactors=FALSE)

> matrix(full[ ,4:11])
     [,1]        
[1,] Numeric,6152
[2,] Numeric,6152
[3,] Numeric,6152
[4,] Numeric,6152
[5,] Numeric,6152
[6,] Numeric,6152
[7,] Numeric,6152
[8,] Numeric,6152
> dim(matrix(full[ ,4:11]))
[1] 8 1

any suggestions?

1 个解决方案

#1


Your matrix doesn't become a vector of vectors. It becomes a matrix of lists having 8 rows. This happens since you supply a list (data.frame) that contains 8 elements as the data argument to matrix(), which then creates a matrix of the list with a default of 1 column.

你的矩阵不会成为矢量的矢量。它成为具有8行的列表矩阵。发生这种情况是因为您提供了一个包含8个元素的列表(data.frame)作为matrix()的数据参数,然后创建一个默认为1列的列表矩阵。

You basically did:

你基本上做了:

matrix(list(1:3,2:4,3:5))
     [,1]     
[1,] Integer,3
[2,] Integer,3
[3,] Integer,3

as @MrFlick notice in the comments you probably want as.matrix(full[ ,4:11]), this will convert your subset, full[ ,4:11], with class data.frame into a matrix of atomic elements following R's coercion hierarchy.

在你可能想要as.matrix(full [,4:11])的评论中注意@MrFlick,这将把你的子集full [,4:11]和class data.frame转换成R之后的原子元素矩阵强制等级。


推荐阅读
author-avatar
蜗牛闯红灯
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有