为什么matrix()和array()返回的对象的类和模式相同?
下面是我的大型数据文件的前几行:
Below are the first few rows of my large data file:
Symbol|Security Name|Market Category|Test Issue|Financial Status|Round Lot Size
AAC|Australia Acquisition Corp. - Ordinary Shares|S|N|D|100
AACC|Asset Acceptance Capital Corp. - Common Stock|Q|N|N|100
AACOU|Australia Acquisition Corp. - Unit|S|N|N|100
AACOW|Australia Acquisition Corp. - Warrant|S|N|N|100
AAIT|iShares MSCI All Country Asia Information Technology Index Fund|G|N|N|100
AAME|Atlantic American Corporation - Common Stock|G|N|N|100
我读取了以下数据:
data <- read.table("nasdaqlisted.txt", sep="|", quote='', header=TRUE, as.is=TRUE)
并构造一个数组和矩阵:
and construct an array and a matrix:
d1 <- array(data, dim=c(nrow(data), ncol(data)))
d2 <- matrix(data, nrow=nrow(data), ncol=ncol(data))
但是,即使 d1
是一个数组, d2
是一个矩阵,类
和模式
相同:
However, even though d1
is an array and d2
is a matrix, the class
and mode
are the same:
> class(d1)
[1] "matrix"
> mode(d1)
[1] "list"
> class(d2)
[1] "matrix"
> mode(d2)
[1] "list"
这是为什么?
我会咬一口,然后去解释我对问题的理解。
I'll bite and have a go at explaining my understanding of the issues.
您不需要大的测试文件即可演示该问题。一个简单的 data.frame
可以做到:
You don't need your large test file to demonstrate the issue. A simple data.frame
would do:
test <- data.frame(var1=1:2,var2=letters[1:2])
> test
var1 var2
1 1 a
2 2 b
请记住, data.frame
只是内部的列表
。
> is.data.frame(test)
[1] TRUE
> is.list(test)
[1] TRUE
有像您期望的那样列出
结构。
> str(test)
'data.frame': 2 obs. of 2 variables:
$ var1: int 1 2
$ var2: Factor w/ 2 levels "a","b": 1 2
> str(as.list(test))
List of 2
$ var1: int [1:2] 1 2
$ var2: Factor w/ 2 levels "a","b": 1 2
当您指定矩阵
调用 data.frame
或 list
,最终得到一个填充有数据元素的矩阵。
When you specify a matrix
call against a data.frame
or a list
, you end up with a matrix filled with the elements of the data.frame or list.
result1 <- matrix(test)
> result1
[,1]
[1,] Integer,2
[2,] factor,2
看看 result1
的结构,您仍然可以看到它仍然是列表
,但现在只有尺寸(请参见下面输出中的最后一行)。
Looking at the structure of result1
, you can see it is still a list
, but now just with dimensions (see the last line in the output below).
> str(result1)
List of 2
$ : int [1:2] 1 2
$ : Factor w/ 2 levels "a","b": 1 2
- attr(*, "dim")= int [1:2] 2 1
现在既是矩阵
和列表
> is.matrix(result1)
[1] TRUE
> is.list(result1)
[1] TRUE
如果从中剥离尺寸对象,它将不再是矩阵
,而是将恢复为只是列表
。
If you strip the dimensions from this object, it will no longer be a matrix
and will revert to just being a list
.
dim(result1) <- NULL
> result1
[[1]]
[1] 1 2
[[2]]
[1] a b
Levels: a b
> is.matrix(result1)
[1] FALSE
> is.list(result1)
[1] TRUE
> str(result1)
List of 2
$ : int [1:2] 1 2
$ : Factor w/ 2 levels "a","b": 1 2