converting a dgCMatrix to data frame












1















I would like to convert a sparse matrix into a data frame of the type (row,column, value). I have found questions such as http://r.789695.n4.nabble.com/Converting-sparse-matrix-to-data-frame-in-Matrix-package-td2332012.html that in the question start with row,column,value and create a sparse matrix. I want the inverse, and I cannot use the as.matrix function, because the matrix is too large. Here is a small example.



r = c(1,2,2,3,3)
c = c(4,1,2,3,5)
v = c(1,2,1,3,1)

a = sparseMatrix(i=r,j=c,x=v)

3 x 5 sparse Matrix of class "dgCMatrix"

[1,] . . . 1 .
[2,] 2 1 . . .
[3,] . . 3 . 1


Can I get a data.frame



  r c v
1 1 4 1
2 2 1 2
3 2 2 1
4 3 3 3
5 3 5 1


Thank you










share|improve this question























  • for calculating inverse, as you mention, solve() doesn't work? (a %*% t(a)) %>% solve seems to return a sparseMatrix object. Unless you need generalized inverse?

    – Nutle
    Nov 26 '18 at 18:25











  • not a matrix inverse, the question I pointed to started with vectors to create a data frame, then created a dense matrix. I wanted to start with a sparse matrix, and get a data frame of (row,col,value). I want to invert his problem, not invert the matrix.

    – HowYaDoing
    Nov 26 '18 at 20:00













  • oh, I see. Still, it's an open question (for me) of how to use generic matrix methods (such as MASS::ginv, for example), with sparseMatrix objects without resolving to as.matrix. I thought you were after a similar thing, but sadly the accepted answer does not solve it.

    – Nutle
    Nov 26 '18 at 20:03


















1















I would like to convert a sparse matrix into a data frame of the type (row,column, value). I have found questions such as http://r.789695.n4.nabble.com/Converting-sparse-matrix-to-data-frame-in-Matrix-package-td2332012.html that in the question start with row,column,value and create a sparse matrix. I want the inverse, and I cannot use the as.matrix function, because the matrix is too large. Here is a small example.



r = c(1,2,2,3,3)
c = c(4,1,2,3,5)
v = c(1,2,1,3,1)

a = sparseMatrix(i=r,j=c,x=v)

3 x 5 sparse Matrix of class "dgCMatrix"

[1,] . . . 1 .
[2,] 2 1 . . .
[3,] . . 3 . 1


Can I get a data.frame



  r c v
1 1 4 1
2 2 1 2
3 2 2 1
4 3 3 3
5 3 5 1


Thank you










share|improve this question























  • for calculating inverse, as you mention, solve() doesn't work? (a %*% t(a)) %>% solve seems to return a sparseMatrix object. Unless you need generalized inverse?

    – Nutle
    Nov 26 '18 at 18:25











  • not a matrix inverse, the question I pointed to started with vectors to create a data frame, then created a dense matrix. I wanted to start with a sparse matrix, and get a data frame of (row,col,value). I want to invert his problem, not invert the matrix.

    – HowYaDoing
    Nov 26 '18 at 20:00













  • oh, I see. Still, it's an open question (for me) of how to use generic matrix methods (such as MASS::ginv, for example), with sparseMatrix objects without resolving to as.matrix. I thought you were after a similar thing, but sadly the accepted answer does not solve it.

    – Nutle
    Nov 26 '18 at 20:03
















1












1








1








I would like to convert a sparse matrix into a data frame of the type (row,column, value). I have found questions such as http://r.789695.n4.nabble.com/Converting-sparse-matrix-to-data-frame-in-Matrix-package-td2332012.html that in the question start with row,column,value and create a sparse matrix. I want the inverse, and I cannot use the as.matrix function, because the matrix is too large. Here is a small example.



r = c(1,2,2,3,3)
c = c(4,1,2,3,5)
v = c(1,2,1,3,1)

a = sparseMatrix(i=r,j=c,x=v)

3 x 5 sparse Matrix of class "dgCMatrix"

[1,] . . . 1 .
[2,] 2 1 . . .
[3,] . . 3 . 1


Can I get a data.frame



  r c v
1 1 4 1
2 2 1 2
3 2 2 1
4 3 3 3
5 3 5 1


Thank you










share|improve this question














I would like to convert a sparse matrix into a data frame of the type (row,column, value). I have found questions such as http://r.789695.n4.nabble.com/Converting-sparse-matrix-to-data-frame-in-Matrix-package-td2332012.html that in the question start with row,column,value and create a sparse matrix. I want the inverse, and I cannot use the as.matrix function, because the matrix is too large. Here is a small example.



r = c(1,2,2,3,3)
c = c(4,1,2,3,5)
v = c(1,2,1,3,1)

a = sparseMatrix(i=r,j=c,x=v)

3 x 5 sparse Matrix of class "dgCMatrix"

[1,] . . . 1 .
[2,] 2 1 . . .
[3,] . . 3 . 1


Can I get a data.frame



  r c v
1 1 4 1
2 2 1 2
3 2 2 1
4 3 3 3
5 3 5 1


Thank you







r sparse-matrix






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 17:23









HowYaDoingHowYaDoing

495311




495311













  • for calculating inverse, as you mention, solve() doesn't work? (a %*% t(a)) %>% solve seems to return a sparseMatrix object. Unless you need generalized inverse?

    – Nutle
    Nov 26 '18 at 18:25











  • not a matrix inverse, the question I pointed to started with vectors to create a data frame, then created a dense matrix. I wanted to start with a sparse matrix, and get a data frame of (row,col,value). I want to invert his problem, not invert the matrix.

    – HowYaDoing
    Nov 26 '18 at 20:00













  • oh, I see. Still, it's an open question (for me) of how to use generic matrix methods (such as MASS::ginv, for example), with sparseMatrix objects without resolving to as.matrix. I thought you were after a similar thing, but sadly the accepted answer does not solve it.

    – Nutle
    Nov 26 '18 at 20:03





















  • for calculating inverse, as you mention, solve() doesn't work? (a %*% t(a)) %>% solve seems to return a sparseMatrix object. Unless you need generalized inverse?

    – Nutle
    Nov 26 '18 at 18:25











  • not a matrix inverse, the question I pointed to started with vectors to create a data frame, then created a dense matrix. I wanted to start with a sparse matrix, and get a data frame of (row,col,value). I want to invert his problem, not invert the matrix.

    – HowYaDoing
    Nov 26 '18 at 20:00













  • oh, I see. Still, it's an open question (for me) of how to use generic matrix methods (such as MASS::ginv, for example), with sparseMatrix objects without resolving to as.matrix. I thought you were after a similar thing, but sadly the accepted answer does not solve it.

    – Nutle
    Nov 26 '18 at 20:03



















for calculating inverse, as you mention, solve() doesn't work? (a %*% t(a)) %>% solve seems to return a sparseMatrix object. Unless you need generalized inverse?

– Nutle
Nov 26 '18 at 18:25





for calculating inverse, as you mention, solve() doesn't work? (a %*% t(a)) %>% solve seems to return a sparseMatrix object. Unless you need generalized inverse?

– Nutle
Nov 26 '18 at 18:25













not a matrix inverse, the question I pointed to started with vectors to create a data frame, then created a dense matrix. I wanted to start with a sparse matrix, and get a data frame of (row,col,value). I want to invert his problem, not invert the matrix.

– HowYaDoing
Nov 26 '18 at 20:00







not a matrix inverse, the question I pointed to started with vectors to create a data frame, then created a dense matrix. I wanted to start with a sparse matrix, and get a data frame of (row,col,value). I want to invert his problem, not invert the matrix.

– HowYaDoing
Nov 26 '18 at 20:00















oh, I see. Still, it's an open question (for me) of how to use generic matrix methods (such as MASS::ginv, for example), with sparseMatrix objects without resolving to as.matrix. I thought you were after a similar thing, but sadly the accepted answer does not solve it.

– Nutle
Nov 26 '18 at 20:03







oh, I see. Still, it's an open question (for me) of how to use generic matrix methods (such as MASS::ginv, for example), with sparseMatrix objects without resolving to as.matrix. I thought you were after a similar thing, but sadly the accepted answer does not solve it.

– Nutle
Nov 26 '18 at 20:03














1 Answer
1






active

oldest

votes


















3














You can use



b = as.data.frame(summary(a))
# i j x
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 4 1 4 1
# 5 3 5 1


If you need the same order as in your example, you can use



b = b[order(b$i),]
# i j x
# 4 1 4 1
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 5 3 5 1


Another alternative, though not quite as neat, is to use



b = as(a, "dgTMatrix")
cbind.data.frame(r = b@i + 1, c = b@j + 1, x = b@x)





share|improve this answer


























  • Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

    – Nutle
    Nov 26 '18 at 18:17











  • also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

    – Nutle
    Nov 26 '18 at 18:21











  • Thanks. That is exactly what I was looking for!!!

    – HowYaDoing
    Nov 26 '18 at 19:54











  • @Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

    – dww
    Nov 29 '18 at 15:15











  • I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

    – Nutle
    Nov 29 '18 at 15:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486135%2fconverting-a-dgcmatrix-to-data-frame%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can use



b = as.data.frame(summary(a))
# i j x
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 4 1 4 1
# 5 3 5 1


If you need the same order as in your example, you can use



b = b[order(b$i),]
# i j x
# 4 1 4 1
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 5 3 5 1


Another alternative, though not quite as neat, is to use



b = as(a, "dgTMatrix")
cbind.data.frame(r = b@i + 1, c = b@j + 1, x = b@x)





share|improve this answer


























  • Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

    – Nutle
    Nov 26 '18 at 18:17











  • also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

    – Nutle
    Nov 26 '18 at 18:21











  • Thanks. That is exactly what I was looking for!!!

    – HowYaDoing
    Nov 26 '18 at 19:54











  • @Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

    – dww
    Nov 29 '18 at 15:15











  • I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

    – Nutle
    Nov 29 '18 at 15:30
















3














You can use



b = as.data.frame(summary(a))
# i j x
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 4 1 4 1
# 5 3 5 1


If you need the same order as in your example, you can use



b = b[order(b$i),]
# i j x
# 4 1 4 1
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 5 3 5 1


Another alternative, though not quite as neat, is to use



b = as(a, "dgTMatrix")
cbind.data.frame(r = b@i + 1, c = b@j + 1, x = b@x)





share|improve this answer


























  • Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

    – Nutle
    Nov 26 '18 at 18:17











  • also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

    – Nutle
    Nov 26 '18 at 18:21











  • Thanks. That is exactly what I was looking for!!!

    – HowYaDoing
    Nov 26 '18 at 19:54











  • @Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

    – dww
    Nov 29 '18 at 15:15











  • I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

    – Nutle
    Nov 29 '18 at 15:30














3












3








3







You can use



b = as.data.frame(summary(a))
# i j x
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 4 1 4 1
# 5 3 5 1


If you need the same order as in your example, you can use



b = b[order(b$i),]
# i j x
# 4 1 4 1
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 5 3 5 1


Another alternative, though not quite as neat, is to use



b = as(a, "dgTMatrix")
cbind.data.frame(r = b@i + 1, c = b@j + 1, x = b@x)





share|improve this answer















You can use



b = as.data.frame(summary(a))
# i j x
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 4 1 4 1
# 5 3 5 1


If you need the same order as in your example, you can use



b = b[order(b$i),]
# i j x
# 4 1 4 1
# 1 2 1 2
# 2 2 2 1
# 3 3 3 3
# 5 3 5 1


Another alternative, though not quite as neat, is to use



b = as(a, "dgTMatrix")
cbind.data.frame(r = b@i + 1, c = b@j + 1, x = b@x)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 17:56

























answered Nov 26 '18 at 17:45









dwwdww

15.2k22656




15.2k22656













  • Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

    – Nutle
    Nov 26 '18 at 18:17











  • also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

    – Nutle
    Nov 26 '18 at 18:21











  • Thanks. That is exactly what I was looking for!!!

    – HowYaDoing
    Nov 26 '18 at 19:54











  • @Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

    – dww
    Nov 29 '18 at 15:15











  • I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

    – Nutle
    Nov 29 '18 at 15:30



















  • Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

    – Nutle
    Nov 26 '18 at 18:17











  • also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

    – Nutle
    Nov 26 '18 at 18:21











  • Thanks. That is exactly what I was looking for!!!

    – HowYaDoing
    Nov 26 '18 at 19:54











  • @Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

    – dww
    Nov 29 '18 at 15:15











  • I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

    – Nutle
    Nov 29 '18 at 15:30

















Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

– Nutle
Nov 26 '18 at 18:17





Any idea whether this approach is indeed cheaper than as.matrix %>% as.data.frame approach? (colnames doesn't matter)

– Nutle
Nov 26 '18 at 18:17













also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

– Nutle
Nov 26 '18 at 18:21





also, in the first case b %*% t(b) returns the following error:requires numeric/complex matrix/vector arguments. I don't see how can we avoid as.matrix here

– Nutle
Nov 26 '18 at 18:21













Thanks. That is exactly what I was looking for!!!

– HowYaDoing
Nov 26 '18 at 19:54





Thanks. That is exactly what I was looking for!!!

– HowYaDoing
Nov 26 '18 at 19:54













@Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

– dww
Nov 29 '18 at 15:15





@Nutle your comments are't really applicable here. As noted already by OP you seem to have mistaken that the question is about inverse matrices - it is not. Maybe consider deleting your comments in case they could inadvertently be misleading to other readers?

– dww
Nov 29 '18 at 15:15













I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

– Nutle
Nov 29 '18 at 15:30





I will cite the exact words from the OP's question: and I cannot use the as.matrix function, because the matrix is too large.

– Nutle
Nov 29 '18 at 15:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486135%2fconverting-a-dgcmatrix-to-data-frame%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)