为什么matrix()和array()返回的对象的类和模式相同?

为什么matrix()和array()返回的对象的类和模式相同?,r,class,R,Class,下面是我的大数据文件的前几行: 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.

下面是我的大数据文件的前几行:

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)

为什么会这样

我会咬一口,试着解释一下我对这些问题的理解

您不需要大型测试文件来演示该问题。一个简单的
data.frame
可以:

test <- data.frame(var1=1:2,var2=letters[1:2])

> test
  var1 var2
1    1    a
2    2    b
如您所料,具有类似于
列表的结构

> 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
指定
matrix
调用时,您将得到一个填充了data.frame或list元素的矩阵

result1 <- matrix(test)

> result1
     [,1]     
[1,] Integer,2
[2,] factor,2 
这意味着它现在既是一个
矩阵
又是一个
列表

> is.matrix(result1)
[1] TRUE
> is.list(result1)
[1] TRUE
如果从该对象中删除尺寸标注,它将不再是
矩阵
,而将恢复为仅是
列表

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
dim(result1)result1
[[1]]
[1] 1 2
[[2]]
[1] a b
级别:a b
>is.矩阵(结果1)
[1] 假的
>is.list(结果1)
[1] 真的
>str(结果1)
2人名单
$:int[1:2]12
$:系数w/2“a”、“b”级:1 2
result1 <- matrix(test)

> result1
     [,1]     
[1,] Integer,2
[2,] factor,2 
> 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
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