Reputation: 2584
I have a list data like this
library(Matrix)
myd<- new("dgTMatrix", i = c(3L, 4L, 4L), j = 0:2, Dim = c(5L, 3L),
Dimnames = list(c("Xkr4", "Rp1", "Sox17", "Mrpl15", "Lypla1"
), c("AAAGTAGAGATGCCAG-1", "AACCGCGTCCAACCAA-1", "AACTCCCGTCGGGTCT-1"
)), x = c(1, 2, 1), factors = list())
I am more comfortable to work with data frame than list, so I am trying to convert it to a data frame but I am not sure if I am destroying the structure or not
I simply do
mydf <- as.data.frame(myd)
then it becomes like this
mydf<- structure(list(`AAAGTAGAGATGCCAG-1` = c(0, 0, 0, 1, 0), `AACCGCGTCCAACCAA-1` = c(0,
0, 0, 0, 2), `AACTCCCGTCGGGTCT-1` = c(0, 0, 0, 0, 1)), class = "data.frame", row.names = c("Xkr4",
"Rp1", "Sox17", "Mrpl15", "Lypla1"))
Do you think there is a safer way to convert such a data?
I tried to understand the data, so if I unlist
it, it will look like below, but still I am not sure of converting
unlist(myd)
5 x 3 sparse Matrix of class "dgTMatrix"
AAAGTAGAGATGCCAG-1 AACCGCGTCCAACCAA-1 AACTCCCGTCGGGTCT-1
Xkr4 . . .
Rp1 . . .
Sox17 . . .
Mrpl15 1 . .
Lypla1 . 2 1
Upvotes: 1
Views: 77
Reputation: 657
There are a couple issues here. First off, calling as.data.frame(myd)
where myd
is an object with class dgTMatrix
throws an error as shown below:
library(Matrix)
myd<- new("dgTMatrix", i = c(3L, 4L, 4L), j = 0:2, Dim = c(5L, 3L),
Dimnames = list(c("Xkr4", "Rp1", "Sox17", "Mrpl15", "Lypla1"
), c("AAAGTAGAGATGCCAG-1", "AACCGCGTCCAACCAA-1", "AACTCCCGTCGGGTCT-1"
)), x = c(1, 2, 1), factors = list())
as.data.frame(myd)
#> Error in as.data.frame.default(myd): cannot coerce class 'structure("dgTMatrix", package = "Matrix")' to a data.frame
The reason for this is that there is no as.data.frame
method defined for dgTMatrix
. However, there is an as.matrix
method defined for dgTMatrix
as well as an as.data.frame
method defined for class matrix
. So, if your best course of action really is to be transitioning between dgTMatrix
and data.frame
(which I feel it may not be), you probably want to do something like:
library(Matrix)
myd<- new("dgTMatrix", i = c(3L, 4L, 4L), j = 0:2, Dim = c(5L, 3L),
Dimnames = list(c("Xkr4", "Rp1", "Sox17", "Mrpl15", "Lypla1"
), c("AAAGTAGAGATGCCAG-1", "AACCGCGTCCAACCAA-1", "AACTCCCGTCGGGTCT-1"
)), x = c(1, 2, 1), factors = list())
mydf <- as.data.frame(as.matrix(myd))
mydf
#> AAAGTAGAGATGCCAG-1 AACCGCGTCCAACCAA-1 AACTCCCGTCGGGTCT-1
#> Xkr4 0 0 0
#> Rp1 0 0 0
#> Sox17 0 0 0
#> Mrpl15 1 0 0
#> Lypla1 0 2 1
Upvotes: 2