问题
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.
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))
I have tried the following:
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")))
回答1:
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)
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))
来源:https://stackoverflow.com/questions/39064869/making-a-matrix-of-a-specific-structure