On Aug 19, 2010, at 7:56 PM, erickso...@aol.com wrote:
Is there a way to get the coordinate list vectors back from the
matrix a without any prior knowledge?
a
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 1 0
[2,] 2 1 0 0 0
[3,] 0 0 3 0 1
How to get:
r = c(1,2,2,3,3)
c = c(4,1,2,3,5)
v = c(1,2,1,3,1)
?sparseMatrix # which has a See Also: "Consider CsparseMatrix and
similar class definition help files."
?"CsparseMatrix-class"
So ... not exactly in that order but "fixing" that problem (of column-
oriented format) is reasonably trivial:
> r= a...@i +1
> r
[1] 2 2 3 1 3
> c= a...@p[1:(length(a...@p)-1)]+1
> c # noting that the choice of "c" for an object name seems a
really bad idea.
[1] 1 2 3 4 5
> v = a...@x
> v
[1] 2 1 3 1 1
---- --- ----
> r[order(r)]
[1] 1 2 2 3 3
> c[order(r)]
[1] 4 1 2 3 5
> v[order(r)]
[1] 1 2 1 3 1
--
David.
-----Original Message-----
From: Phil Spector <spec...@stat.berkeley.edu>
To: erickso...@aol.com
Cc: r-help@r-project.org
Sent: Thu, Aug 19, 2010 7:35 pm
Subject: Re: [R] Converting sparse matrix to data.frame in Matrix
package
This seems to work, although it eliminates the sparseness of the
storage:
dimnames(a) = list(NULL,letters[1:5])
as.data.frame(as.matrix(a))
a b c d e
1 0 0 0 1 0
2 2 1 0 0 0
3 0 0 3 0 1
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spec...@stat.berkeley.edu
On Thu, 19 Aug 2010, erickso...@aol.com wrote:
I am able to create a coordinate list sparse matrix this way:
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)
However, this results in an object that looks like this:
a
3 x 5 sparse Matrix of class "dgCMatrix"
[1,] . . . 1 .
[2,] 2 1 . . .
[3,] . . 3 . 1
How do I convert this object into a data.frame that would look like
this:
data.frame(a=c(0,2,0),b=c(0,1,0),c=c(0,0,3),d=c(1,0,0),e=c(0,0,1))
a b c d e
1 0 0 0 1 0
2 2 1 0 0 0
3 0 0 3 0 1
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.