制作特定结构的矩阵 [英] Making a matrix of a specific structure

查看:103
本文介绍了制作特定结构的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:我不知道我在哪里错误,但是我一整天都在努力解决这个问题。所以,我要求不把它丢弃为重复的问题,并将其视为与矩阵结构相关的一个非常具体的问题。

Please note: I don't know where I am mistaking but I have spent whole day trying to solve this problem. So, I request not to discard it as duplicate question and treat this as a very specific question related to matrix structure.

我有以下data.frame:

I have the following data.frame:

dput(c.m.q)

structure(list(ASK_Price = c(1801, 1687.3, 1687.2, 1688.95, 1687.15, 
1688.95, 1687.5, 1688.85, 1689, 1688.95, 1687.5, 1688, 1688, 
1687.5, 1689.95, 1685.85, 1689.9, 1689.95, 1688.8, 1688.95, 1687.15, 
1687.2, 1690, 1688, 1688, 1690, 1688.7, 1690, 1688.7, 1689.9, 
1688.7, 1689.9, 1688.7, 1689.9, 1689.8, 1687.95, 1689.75, 1689.7, 
1687.95, 1689.35, 1689.3), BID_Price = c(1651, 1688.5, 1687, 
1688.5, 1687, 1688.5, 1687.05, 1688.5, 1687.05, 1687.05, 1688.5, 
1688.5, 1688.5, 1688.5, 1688.7, 1685.35, 1688.2, 1688.95, 1685.6, 
1685.6, 1689.05, 1685.8, 1689.1, 1686.1, 1686.25, 1689.95, 1688.05, 
1689.95, 1688.1, 1689.8, 1688.1, 1689.8, 1686.1, 1689.8, 1689.7, 
1686.15, 1689.7, 1689.15, 1686.15, 1689.15, 1689.15)), .Names = c("ASK_Price", 
"BID_Price"), row.names = c(1L, 17704L, 17707L, 17708L, 17709L, 
17710L, 17713L, 17714L, 17717L, 17720L, 17729L, 17732L, 17735L, 
17738L, 17983L, 17984L, 17985L, 17986L, 18015L, 18018L, 18029L, 
18032L, 18033L, 18223L, 18225L, 18226L, 18227L, 18228L, 18229L, 
18230L, 18231L, 18232L, 18233L, 18234L, 18235L, 18236L, 18237L, 
18238L, 18239L, 18240L, 18241L), class = "data.frame")

我希望在结构中看起来类似于这样:

I want it to look similar to this in structure:

dput(mydata)

structure(c(4.56, 4.57, 4.53, 4.59, 4.55, 4.59, 4.59, 4.55, 4.55, 
4.55, 4.59, 4.55, 4.59, 4.59, 4.7, 4.64, 4.65, 4.66, 4.65, 4.66, 
4.66, 4.65, 4.65, 4.65, 4.66, 4.65, 4.66, 4.66), .Dim = c(14L, 2L))

我尝试过以下操作:

c.m.q<-as.matrix(sapply(c.m.q, as.numeric))

但是我不会得到类似的矩阵结构:

But I don't get the similar matrix strucutre:

structure(c(1801, 1687.3, 1687.2, 1688.95, 1687.15, 1688.95, 
1687.5, 1688.85, 1689, 1688.95, 1687.5, 1688, 1688, 1687.5, 1689.95, 
1685.85, 1689.9, 1689.95, 1688.8, 1688.95, 1687.15, 1687.2, 1690, 
1688, 1688, 1690, 1688.7, 1690, 1688.7, 1689.9, 1688.7, 1689.9, 
1688.7, 1689.9, 1689.8, 1687.95, 1689.75, 1689.7, 1687.95, 1689.35, 
1689.3, 1651, 1688.5, 1687, 1688.5, 1687, 1688.5, 1687.05, 1688.5, 
1687.05, 1687.05, 1688.5, 1688.5, 1688.5, 1688.5, 1688.7, 1685.35, 
1688.2, 1688.95, 1685.6, 1685.6, 1689.05, 1685.8, 1689.1, 1686.1, 
1686.25, 1689.95, 1688.05, 1689.95, 1688.1, 1689.8, 1688.1, 1689.8, 
1686.1, 1689.8, 1689.7, 1686.15, 1689.7, 1689.15, 1686.15, 1689.15, 
1689.15), .Dim = c(41L, 2L), .Dimnames = list(NULL, c("ASK_Price", "BID_Price")))


推荐答案

转换成矩阵后, dimnames 变为NULL(假设OP中的预期输出显示在没有任何亮点的OP的帖子中)

We can set the dimnames to NULL after converting to matrix (assuming from the expected output showed in the OP's post without any dimnames)

m1 <- `dimnames<-`(as.matrix(c.m.q), NULL)

如果我们检查 dput 'm1',结构相似

If we check the dput of 'm1', it is of similar structure

dput(m1)

structure(c(1801, 1687.3, 1687.2, 1688.95, 1687.15, 1688.95, 
1687.5, 1688.85, 1689, 1688.95, 1687.5, 1688, 1688, 1687.5, 1689.95, 
1685.85, 1689.9, 1689.95, 1688.8, 1688.95, 1687.15, 1687.2, 1690, 
1688, 1688, 1690, 1688.7, 1690, 1688.7, 1689.9, 1688.7, 1689.9, 
1688.7, 1689.9, 1689.8, 1687.95, 1689.75, 1689.7, 1687.95, 1689.35, 
1689.3, 1651, 1688.5, 1687, 1688.5, 1687, 1688.5, 1687.05, 1688.5, 
1687.05, 1687.05, 1688.5, 1688.5, 1688.5, 1688.5, 1688.7, 1685.35, 
1688.2, 1688.95, 1685.6, 1685.6, 1689.05, 1685.8, 1689.1, 1686.1, 
1686.25, 1689.95, 1688.05, 1689.95, 1688.1, 1689.8, 1688.1, 1689.8, 
1686.1, 1689.8, 1689.7, 1686.15, 1689.7, 1689.15, 1686.15, 1689.15, 
1689.15), .Dim = c(41L, 2L))

这篇关于制作特定结构的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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