[R] moving dimnames when reassigning

Pierre Kleiber pkleiber at honlab.nmfs.hawaii.edu
Tue Oct 30 21:17:26 CET 2001


It's not a bug IMHO.  If you want to switch columns as you indicated,
try this:
   > t1 <- t1[,c(2,1,3:5)]
    or
   > t1 <- t1[,c("B","A","C","D","E")]

The approach for rows would be similar.

Cheers, Pierre


"Wiener, Matthew Charles" wrote:
> 
> Hi, all.
> 
> I've got a question that arose when I was switching some row and columns in
> a matrix.  I was expecting the row and column names to follow their rows and
> columns, and they didn't.  I think I see some logic to the behavior, but I'm
> not sure whether it's intentional or not, so I thought I'd ask the list
> whether this is a feature or a bug.
> 
> Here's an example (R-1.3.1, same behavior under Windows NT and Linux).  This
> example uses a matrix, and switches columns.  The same thing happens when
> using data frames and when switching rows.
> 
> > t1 <- matrix(runif(25), 5, 5)
> > dimnames(t1) <- list(letters[1:5], LETTERS[1:5])
> > t1
>           A         B                C          D          E
> a 0.8857528 0.1435165 0.44589190 0.1837626 0.000864055
> b 0.1867816 0.8102641 0.97127777 0.8952994 0.151531510
> c 0.1102591 0.4704422 0.04712121 0.2345568 0.535079125
> d 0.7435725 0.7270509 0.49541471 0.3195482 0.892261212
> e 0.7610705 0.6057041 0.73183485 0.9959111 0.176386124
> >
> > t1[,c(1,2)] <- t1[,c(2,1)]
> > t1
>           A         B                C                  D           E
> a 0.1435165 0.8857528 0.44589190 0.1837626 0.000864055
> The columns switched,
> b 0.8102641 0.1867816 0.97127777 0.8952994 0.151531510          but the
> column names
> c 0.4704422 0.1102591 0.04712121 0.2345568 0.535079125          didn't.
> d 0.7270509 0.7435725 0.49541471 0.3195482 0.892261212
> e 0.6057041 0.7610705 0.73183485 0.9959111 0.176386124
> >
> > t1 <- t1[,c(2,1,3,4,5)]
> >
> > t1
>           B         A                C                  D            E
> a 0.8857528 0.1435165 0.44589190 0.1837626 0.000864055          The columns
> and
> b 0.1867816 0.8102641 0.97127777 0.8952994 0.151531510          column names
> both
> c 0.1102591 0.4704422 0.04712121 0.2345568 0.535079125          switched.
> d 0.7435725 0.7270509 0.49541471 0.3195482 0.892261212
> e 0.7610705 0.6057041 0.73183485 0.9959111 0.176386124
> >
> 
> I was rearranging a matrix in the first way, and then found that the row and
> column names hadn't followed along.  Two workarounds are to permute the row
> and column names separately (which is what I'm doing), or to construct
> indices to do things the second way.
> 
> If this was a design decision, I'd like to understand the reason for it
> better.  If it's unintended, I'll turn this into a bug report.
> 
> Regards,
> 
> Matt Wiener
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
-----------------------------------------------------------------
Pierre Kleiber             Email: pkleiber at honlab.nmfs.hawaii.edu
Fishery Biologist                     Tel: 808 983-5399/737-7544
NOAA FISHERIES - Honolulu Laboratory         Fax: 808 983-2902
2570 Dole St., Honolulu, HI 96822-2396 
-----------------------------------------------------------------
 "God could have told Moses about galaxies and mitochondria and
  all.  But behold... It was good enough for government work."
-----------------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list