[R] slow access to matrix dimnames

From: Dan Dube <ddube_at_advisen.com>
Date: Fri, 4 Jan 2008 14:29:09 -0500


i have been trying to convert my data frames to matrices in the hopes of speeding up some of my more complicated scripts.

to assist with this, i am trying to create a "matrix column operator" like $:
"%$%" = function(data,field) {

        as.numeric(data[,grep(field,unlist(dimnames(data)[2]))]) }

the idea here is that you can use a matrix like a dataframe: matrix%$%"fieldname"

i am getting this matrix by converting a dataframe: df = read.csv("data.csv")
matrix = data.matrix(df,rownames.force=FALSE)

this sets rownames to "NULL", but there is still an entry at dimnames(matrix)[1], and so i have to access the actual column names as dimnames(matrix)[2]. if there were only one dimension of dimnames, this operator works quickly, but when i have to access [2], it is super slow.

am i way off base trying to do this? i'd like to have the ability to talk about the columns by name, since they may not always be in the same place. maybe i am making it more complicated than necessary?



R-help_at_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. Received on Fri 04 Jan 2008 - 19:37:22 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Jan 2008 - 22:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive