I have a 47k x 47k adjacency matrix that is very sparse (at most 30 entries per row); my textual representation therefore is simply an adjacency list of connections between nodes for each row, e.g. node connections A B C D E B A C D C A E D A E A F F E G H I'd like to import this into a dataframe of node/connection (character/vector-of-characters) pairs. I've experimented with scan, but haven't been able to coax it to work. I can also "hack" it with strsplit() myself, but I thought there might be a more elegant way. Thanks, -Aaron
Have you considered the read.matrix.csr() function in pkg. e1071? It uses another sparse input format, but perhaps you can easily transform your data in the supported one. Also, in my experience, data frames are not the best basis for a sparse format since they might turn out to be very memory consuming and slow... The sparse formats provided by the SparseM package are better suited for this. -d Date: Tue, 27 Apr 2004 17:10:09 -0400 From: "Aaron J. Mackey" <amackey at pcbi.upenn.edu> Subject: [R] reading a "sparse" matrix into R To: r-help at stat.math.ethz.ch Message-ID: <43D15D80-988F-11D8-A84D-000A958C5008 at pcbi.upenn.edu> Content-Type: text/plain; charset=US-ASCII; format=flowed I have a 47k x 47k adjacency matrix that is very sparse (at most 30 entries per row); my textual representation therefore is simply an adjacency list of connections between nodes for each row, e.g. node connections A B C D E B A C D C A E D A E A F F E G H I'd like to import this into a dataframe of node/connection (character/vector-of-characters) pairs. I've experimented with scan, but haven't been able to coax it to work. I can also "hack" it with strsplit() myself, but I thought there might be a more elegant way. Thanks, -Aaron
David Meyer <david.meyer at wu-wien.ac.at> writes:> Have you considered the read.matrix.csr() function in pkg. e1071? It > uses another sparse input format, but perhaps you can easily transform > your data in the supported one. Also, in my experience, data frames are > not the best basis for a sparse format since they might turn out to be > very memory consuming and slow... The sparse formats provided by the > SparseM package are better suited for this.The Matrix package (versions 0.8-1 and later) has a C function that does the opposite operation, converting a symmetric sparse matrix to a graph. I would look at the way that the graph is stored there (the formulation is from the Metis package of C code) and try to convert your adjacency graph to that formulation first.
I've used lists (generic vectors) for this, with integer storage mode. Then I can easily manipulate them in R, they don't take too much room, and C code to traverse them (e.g. find connected components) is fast. Did you have a need for a data frame? That seems like it might be painful to manipulate. I read the text file with readLines. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Aaron J. Mackey Sent: Tuesday, April 27, 2004 5:10 PM To: r-help at stat.math.ethz.ch Subject: [R] reading a "sparse" matrix into R I have a 47k x 47k adjacency matrix that is very sparse (at most 30 entries per row); my textual representation therefore is simply an adjacency list of connections between nodes for each row, e.g. node connections A B C D E B A C D C A E D A E A F F E G H I'd like to import this into a dataframe of node/connection (character/vector-of-characters) pairs. I've experimented with scan, but haven't been able to coax it to work. I can also "hack" it with strsplit() myself, but I thought there might be a more elegant way. Thanks, -Aaron ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html