I have a matrix y:> dimnames(y)$x93 [1] "1" "2" $x94 [1] "0" "1" "2" .................. so on (there are other dimensions as well) I need to access a particular dimension, but a random mechanism tells me which dimension it would. So, sometimes I might need to access dimnames(y)$x93, some other time it would be dimnames(y)$x94.. and so on. Now let that random dimension be idx, then dimnames(y)$paste('x',idx,sep='') doesn't work. Can anyone help? Thanks! [[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 18.12.2007 12:01:41:> I have a matrix y: > > > dimnames(y) > $x93 > [1] "1" "2" > > $x94 > [1] "0" "1" "2" > .................. so on (there are other dimensions as well) > > > > I need to access a particular dimension, but a random mechanism tells me > which dimension it would. So, sometimes I might need to access > dimnames(y)$x93, some other time it would be dimnames(y)$x94.. and soon.> Now let that random dimension be idx, thendimnames(y)$paste('x',idx,sep='')> doesn't work.Why not dimnames(y)[idx] Regards Petr> > Can anyone help? > > Thanks! > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hard to help as i do not have "y" and it definitelly is not a matrix as you tried to pretend. 1. Try to look at structure of your y object by str(y) 2. Try to learn about how to extract parts of objects e.g. by reading ?"[" 3. Try to use what you learned on your y object 4. If you still does not get what you want then make some example which can be reproduced and ask again> mat<-matrix(rnorm(12),3,4) > dmat<-data.frame(mat) > dimnames(dmat)[[1]] [1] "1" "2" "3" [[2]] [1] "X1" "X2" "X3" "X4"> dimnames(dmat)[1][[1]] [1] "1" "2" "3"> dimnames(dmat)[1][1][[1]] [1] "1" "2" "3"> dimnames(dmat)[[1]][1][1] "1" Regards Petr petr.pikal at precheza.cz born.to.b.wyld at gmail.com napsal dne 18.12.2007 14:25:06:> Thanks. Actually, I need something else as well. > > I need to get as.numeric(dimnames(y)$x93[1]), which in this case is 1. Itried> as.numeric(dimnames(y)$paste('x',idx,sep='')[1]), and it did not work. > > Please help. > > >> On Dec 18, 2007 6:26 AM, Petr PIKAL <petr.pikal at precheza.cz> wrote: > Hi > > r-help-bounces at r-project.org napsal dne 18.12.2007 12:01:41: > > > I have a matrix y: > > > > > dimnames(y) > > $x93 > > [1] "1" "2" > > > > $x94 > > [1] "0" "1" "2" > > .................. so on (there are other dimensions as well) > > > > > > > > I need to access a particular dimension, but a random mechanism tellsme> > which dimension it would. So, sometimes I might need to access > > dimnames(y)$x93, some other time it would be dimnames(y)$x94.. and so > on. > > Now let that random dimension be idx, then > dimnames(y)$paste('x',idx,sep='') > > doesn't work.> Why not > > dimnames(y)[idx] > > Regards > Petr > > > > > > Can anyone help? > > > > Thanks! > > > > [[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.
dimnames(y)[[paste('x', idx, sep="")]] On Dec 18, 2007 6:01 AM, <born.to.b.wyld at gmail.com> wrote:> I have a matrix y: > > > dimnames(y) > $x93 > [1] "1" "2" > > $x94 > [1] "0" "1" "2" > .................. so on (there are other dimensions as well) > > > > I need to access a particular dimension, but a random mechanism tells me > which dimension it would. So, sometimes I might need to access > dimnames(y)$x93, some other time it would be dimnames(y)$x94.. and so on. > Now let that random dimension be idx, then dimnames(y)$paste('x',idx,sep='') > doesn't work. > > Can anyone help? > > Thanks! > > [[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
To access your dimension idx you could do either assign("a",paste("dimnames(y)$x",idx,sep="")) or eval(parse(text=paste("a<-dimnames(y)$x",idx,sep=""))) --- born.to.b.wyld at gmail.com wrote:> I have a matrix y: > > > dimnames(y) > $x93 > [1] "1" "2" > > $x94 > [1] "0" "1" "2" > .................. so on (there are other > dimensions as well) > > > > I need to access a particular dimension, but a > random mechanism tells me > which dimension it would. So, sometimes I might need > to access > dimnames(y)$x93, some other time it would be > dimnames(y)$x94.. and so on. > Now let that random dimension be idx, then > dimnames(y)$paste('x',idx,sep='') > doesn't work. > > Can anyone help? > > Thanks! > > [[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. >