Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 制作特定结构的矩阵_R_Matrix_Dataframe - Fatal编程技术网

R 制作特定结构的矩阵

R 制作特定结构的矩阵,r,matrix,dataframe,R,Matrix,Dataframe,请注意:我不知道我错在哪里,但我花了一整天的时间试图解决这个问题。所以,我要求不要把它当作重复的问题,而把它当作一个与矩阵结构有关的非常具体的问题 我有以下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

请注意:我不知道我错在哪里,但我花了一整天的时间试图解决这个问题。所以,我要求不要把它当作重复的问题,而把它当作一个与矩阵结构有关的非常具体的问题

我有以下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")
我希望它在结构上与此类似:

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

我们可以将
dimnames
转换为
matrix
后设置为NULL(假设OP文章中显示的预期输出没有任何dimnames)


@PolarBear在发布的dput中的
n_交易在哪里
,因为我不了解你提到的错误的上下文?@PolarBear,你需要问你真正的问题,而不是你认为能为你解决真正问题的其他问题。
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")))
m1 <- `dimnames<-`(as.matrix(c.m.q), NULL)
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))