matrx的类和模式 [英] The class and mode of a matrx

查看:173
本文介绍了matrx的类和模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 符号|安全名称|财务状况|圆地尺
AAC |澳大利亚收购公司 - 普通股| S | N | D | 100
AACC |资产承兑资本公司 - 普通股| Q | N | N | 100
AACOU |澳大利亚收购公司 - 单位| S | N | N | 100
AACOW |澳大利亚收购公司 - 保证| S | N | N | 100
AAIT | iShares MSCI信息技术指数基金| G | N | N | 100
AAME |大西洋美国公司 - 普通股| G | N | N | 100

我读取的数据:

  data<  -  read.table nasdaqlisted.txt,sep =|,quote ='',header = TRUE,as.is = TRUE)



并构造一个数组和一个矩阵:

  (数据),ncol(数据)))
d2< - matrix(data,nrow = nrow(data),ncol = ncol(data))

但是,即使 d1 是数组, d2 是一个矩阵, class 模式是相同的:

 > class(d1)
[1]matrix
>模式(d1)
[1]list
> class(d2)
[1]matrix
>模式(d2)
[1]list

解决方案

我会咬一口气,解释我对这些问题的理解。



您不需要使用大型测试文件来演示问题。一个简单的 data.frame 会执行:

  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。的2个变量:
$ var1:int 1 2
$ var2:因子w / 2级a,b:1 2

> str(as.list(test))
2的列表
$ var1:int [1:2] 1 2
$ var2:factor w / 2级别a,b :1 2

指定 matrix 调用 data.frame 列表,您将得到一个填充数据元素的矩阵。框架或列表。

  result1<  -  matrix(test)

& result1
[,1]
[1,] Integer,2
[2,] factor,2

查看 result1 的结构,您可以看到它仍然是一个列表,但现在只有维度(见下面输出的最后一行)。

 > str(result1)
2的列表
$:int [1:2] 1 2
$:因子w / 2级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] ab
级别:ab

> is.matrix(result1)
[1] FALSE
> is.list(result1)
[1] TRUE

> str(result1)
2的列表
$:int [1:2] 1 2
$:因子w / 2级a,b:1 2


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

I read the data in:

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))

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"

Why is this?

解决方案

I'll bite and have a go at explaining my understanding of the issues.

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

Keep in mind that a data.frame is just a list internally.

> is.data.frame(test)
[1] TRUE
> is.list(test)
[1] TRUE

With a list-like structure as you would expect.

> 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

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 

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

Which means it is now both a matrix and a list

> 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

这篇关于matrx的类和模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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