Hi, I'm quite new to R, so excuse me if this problem has a simple solution. I'm working with an array, lets say i <- array(c(1:3,3:1), dim=c(3,2)) Then I want to give the rows and the columns names: rownames(i)<-c("a","b","c") colnames(i)<-c("d","e") The result is given below: d e a 1 3 b 2 2 c 3 1 Here comes my problem. When I'm taking a submatrix j<-i[1,1:2] the result should be (for me) an array of one line, and two colums. Here's the result: d e 1 3 When I want to access the rownames of j, it returns NULL. I want it to be "a". On the other side, if I take a submatrix 2x2, there is no problem. In my problem, rownames(j) must return the name of the extracted row. So I don't understand why a 1x2 array is not a normal array. Could someone help me with this? Thanx in advance, Patrick
Dear Patrick, By default, when indexing returns an array dimension of 1, the corresponding coordinate is dropped. Try j <- i[1, 1:2, drop=FALSE], and see ?"[". I hope this helps, John> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Pat Meyer > Sent: Friday, December 17, 2004 9:34 AM > To: r-help at stat.math.ethz.ch > Cc: patrick.meyer at internet.lu > Subject: [R] Matrix and rownames problem > > Hi, > I'm quite new to R, so excuse me if this problem has a simple > solution. > > I'm working with an array, lets say > > i <- array(c(1:3,3:1), dim=c(3,2)) > > Then I want to give the rows and the columns names: > > rownames(i)<-c("a","b","c") > colnames(i)<-c("d","e") > > The result is given below: > > d e > a 1 3 > b 2 2 > c 3 1 > > Here comes my problem. When I'm taking a submatrix > > j<-i[1,1:2] > > the result should be (for me) an array of one line, and two > colums. Here's the result: > > d e > 1 3 > > When I want to access the rownames of j, it returns NULL. I > want it to be "a". > > On the other side, if I take a submatrix 2x2, there is no problem. > > In my problem, rownames(j) must return the name of the > extracted row. So I don't understand why a 1x2 array is not a > normal array. > > Could someone help me with this? > > Thanx in advance, > > Patrick > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
On Fri, 2004-12-17 at 14:34 +0000, Pat Meyer wrote:> Hi, > I'm quite new to R, so excuse me if this problem has a simple solution.It does. It's in FAQ 7.5 "Why do my matrices lose dimensions?"> I'm working with an array, lets say > > i <- array(c(1:3,3:1), dim=c(3,2)) > > Then I want to give the rows and the columns names: > > rownames(i)<-c("a","b","c") > colnames(i)<-c("d","e") > > The result is given below: > > d e > a 1 3 > b 2 2 > c 3 1 > > Here comes my problem. When I'm taking a submatrix > > j<-i[1,1:2]If you just want the first row, you can use: j <- i[1, ]> the result should be (for me) an array of one line, and two colums. Here's > the result: > > d e > 1 3 > > When I want to access the rownames of j, it returns NULL. I want it to be > "a". > > On the other side, if I take a submatrix 2x2, there is no problem. > > In my problem, rownames(j) must return the name of the extracted row. So I > don't understand why a 1x2 array is not a normal array. > > Could someone help me with this? > > Thanx in advance,As per the FAQ referenced above, use:> j <- i[1, , drop = FALSE]> jd e a 1 3 HTH, Marc Schwartz