为什么这个矩阵不是数字的?然后`as.numeric`破坏矩阵并返回向量 [英] Why is this matrix not numeric? Then `as.numeric` destroys the matrix and return a vector

查看:322
本文介绍了为什么这个矩阵不是数字的?然后`as.numeric`破坏矩阵并返回向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为input的数据帧.第一列引用商品ID(ArtID),随后的列将用于创建矩阵.

I have a data frame called input. The first column refers to an Article ID (ArtID), the subsequent columns will be used to create the matrix.

基于ArtID,我希望R生成2x2矩阵(更精确地说:它必须是2x2数字矩阵).具体来说,我想为第一行(ArtID == 1),第二行(ArtID == 2)等等创建一个矩阵...

Based on the ArtID, I want R to generate a 2x2 matrix (more precise: It needs to be a numeric 2x2 matrix). Specifically, I want to create a matrix for the first row (ArtID == 1), the second row(ArtID == 2) and so on...

到目前为止,我想到的是:

What I came up with so far is this:

for(i in 1:3) {stored.matrix = matrix(input[which(ArtID ==i),-1],nrow = 2)

这给了我2x2的矩阵,但是它不是数字的(必须是数字).

This gives me a 2x2 matrix, but it is not numeric (which it needs to be).

如果我应用as.numeric,则该矩阵不再是2x2矩阵.

If I apply as.numeric, the matrix is no longer a 2x2 matrix.

如何获得2x2的数字矩阵?

How do I get a 2x2 numerical matrix?

最小的可复制示例:

ArtID = c(1,2,3)
AC_AC = c(1,1,1)
MKT_AC = c(0.5,0.6,0.2)
AC_MKT = c(0.5,0.6,0.2)
MKT_MKT = c(1,1,1)
input = data.frame(ArtID, AC_AC, MKT_AC, AC_MKT, MKT_MKT)

stored.matrix = matrix(input[which(ArtID ==i),-1],nrow = 2)
#     [,1] [,2]
#[1,] 1    0.5 
#[2,] 0.5  1  

is.numeric(stored.matrix)
# [1] FALSE

as.numeric(stored.matrix)
## [1] 1.0 0.5 0.5 1.0

应用as.numeric()后可以看到,矩阵不再是2x2.

As you can see after applying as.numeric() the matrix is no longer 2x2.

任何人都可以帮忙吗?

推荐答案

当数据框中只有数字值时,使用矩阵更合适.将您的数据帧转换为矩阵将解决所有问题.另外,

when you have only numerical values in your data frame, it is more appropriate to use a matrix. Convert your data frame to a matrix will solve all problem. Also,

input <- data.matrix(input)


ArtID = c(1,2,3)
AC_AC = c(1,1,1)
MKT_AC = c(0.5,0.6,0.2)
AC_MKT = c(0.5,0.6,0.2)
MKT_MKT = c(1,1,1)
input = data.frame(ArtID, AC_AC, MKT_AC, AC_MKT, MKT_MKT)

input <- data.matrix(input)   ## <- this line

stored.matrix = matrix(input[which(ArtID ==i),-1], 2)
is.numeric(stored.matrix)
# [1] TRUE


那是什么问题?

如果input是数据帧,则按行子集设置的input[which(ArtID == i),-1]仍返回数据帧.数据框是列表的一种特殊类型.当您将列表提供给matrix()时,就会遇到矩阵列表的情况.

If input is a data frame, input[which(ArtID == i),-1] by row subsetting still returns a data frame. A data frame is a special type of list. When you feed a list to matrix(), you get into a situation of matrix list.

如果您阅读?matrix可以获取的数据,则会看到:

If you read ?matrix for what data it can take, you will see:

data: an optional data vector (including a list or ‘expression’
      vector).  Non-atomic classed R objects are coerced by
      ‘as.vector’ and all attributes discarded.

请注意,列表也是矢量数据类型(例如,is.vector(list(a = 1))给出TRUE),因此将列表提供给matrix是合法的.你可以尝试

Note that a list is also of vector data type (e.g., is.vector(list(a = 1)) gives TRUE), so it is legitimate to feed a list to matrix. You can try

test <- matrix(list(a = 1, b = 2, c = 3, d = 4), 2)
#     [,1] [,2]
#[1,] 1    3   
#[2,] 2    4   

class(test)给出矩阵"的意义上来说,这确实是一个矩阵,但是

This is indeed a matrix in the sense that class(test) give "matrix"), but

str(test)
#List of 4
# $ : num 1
# $ : num 2
# $ : num 3
# $ : num 4
# - attr(*, "dim")= int [1:2] 2 2

typeof(test)
# [1] "list"

所以它不是我们引用的普通数值矩阵.

so it is not the usual numerical matrix we refer to.

输入列表也可能参差不齐.

The input list can be ragged, too.

test <- matrix(list(a = 1, b = 2:3, c = 4:6, d = 7:10), 2)
#     [,1]      [,2]     
#[1,] 1         Integer,3
#[2,] Integer,2 Integer,4

str(test)
#List of 4
# $ : num 1
# $ : int [1:2] 2 3
# $ : int [1:3] 4 5 6
# $ : int [1:4] 7 8 9 10
# - attr(*, "dim")= int [1:2] 2 2


我想知道为什么typeof()给我列表...:)

And I was wondering why typeof() gives me list... :)

是的,所以已经意识到一些不寻常的事情.矩阵的存储方式取决于其元素的存储方式.对于矩阵列表,元素是列表,因此矩阵具有列表"模式.

Yes, so had realized something unusual. The storage mode of a matrix is determined by that of its element. For a matrix list, elements are list, hence the matrix has "list" mode.

这篇关于为什么这个矩阵不是数字的?然后`as.numeric`破坏矩阵并返回向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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