yoav baranan
2011-Apr-20 19:52 UTC
[R] get cells by the combination of their column and row names
Hi, I have a (correlation) matrix and I want to select a subset of its cells depending on the combination of their column and row names. This illustrates my problem: mtrx <- matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3, dimnames = list(c('c132','c432', 'c233'), c('r132','r233', 'r432')))> mtrx r132 r233 r432c132 1 4 7c432 2 5 8c233 3 6 9 At this point I want to compute the mean of the cells in which the column and the row names share the same suffix (i.e., the cells mtrx[1,1], mtrx[2,3], mtrx[3,2]). By suffix I mean the last three digits of the row/col name. I need a generic code (and not vec<-c(mtrx[1,1], mtrx[2,3], mtrx[3,2])) because I don't know in advance how many rows and columns I will have (currently 118). Thanks, Yoav [[alternative HTML version deleted]]
Phil Spector
2011-Apr-20 20:29 UTC
[R] get cells by the combination of their column and row names
Yoav - Here's one possibility:> wh = outer(rownames(mtrx),colnames(mtrx),+ function(x,y)substr(x,nchar(x)-2,nchar(x)) == substr(y,nchar(y)-2,nchar(y)))> mtrx[wh][1] 1 6 8 If you knew that all of the row and column names were 4 characters long, it would simplify to> mtrx[outer(rownames(mtrx),colnames(mtrx),function(x,y)substr(x,2,4) == substr(y,2,4))]Hope this helps. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 20 Apr 2011, yoav baranan wrote:> > Hi, > I have a (correlation) matrix and I want to select a subset of its cells depending on the combination of their column and row names. > This illustrates my problem: mtrx <- matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3, dimnames = list(c('c132','c432', 'c233'), c('r132','r233', 'r432')))> mtrx r132 r233 r432c132 1 4 7c432 2 5 8c233 3 6 9 > At this point I want to compute the mean of the cells in which the column and the row names share the same suffix (i.e., the cells mtrx[1,1], mtrx[2,3], mtrx[3,2]). By suffix I mean the last three digits of the row/col name. > I need a generic code (and not vec<-c(mtrx[1,1], mtrx[2,3], mtrx[3,2])) because I don't know in advance how many rows and columns I will have (currently 118). > Thanks, Yoav > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
David Winsemius
2011-Apr-20 20:40 UTC
[R] get cells by the combination of their column and row names
On Apr 20, 2011, at 3:52 PM, yoav baranan wrote:> > Hi, > I have a (correlation) matrix and I want to select a subset of its > cells depending on the combination of their column and row names. > This illustrates my problem: mtrx <- matrix(c(1,2,3,4,5,6,7,8,9), > nrow=3, ncol=3, dimnames = list(c('c132','c432', 'c233'), > c('r132','r233', 'r432')))> mtrx r132 r233 r432c132 1 4 > 7c432 2 5 8c233 3 6 9 > At this point I want to compute the mean of the cells in which the > column and the row names share the same suffix (i.e., the cells > mtrx[1,1], mtrx[2,3], mtrx[3,2]). By suffix I mean the last three > digits of the row/col name. > I need a generic code (and not vec<-c(mtrx[1,1], mtrx[2,3], > mtrx[3,2])) because I don't know in advance how many rows and > columns I will have (currently 118).> mtrx[ sub("r|c", "", rep(rownames(mtrx),times=3) ) = sub("r|c", "", rep(colnames(mtrx), each=3))] [1] 1 6 8 > mean( mtrx[ sub("r|c", "", rep(rownames(mtrx),times=3) ) = sub("r|c", "", rep(colnames(mtrx), each=3))] ) [1] 5 Obviously you need to substitute NROW() and NCOL() vlues for the rep arguments but that should be simple.> Thanks, Yoav > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT