[R] moving dimnames when reassigning

Bill.Venables@CMIS.CSIRO.AU Bill.Venables at CMIS.CSIRO.AU
Wed Oct 31 06:11:11 CET 2001


This is an instructive example in a minor sort of way; it often clears
things up quite a bit once this lesson is learnt.

An assignment of the form

> name <- value

assigns the name to the value.  An assignment of the form

> name[] <- value

(effectively) uses value to replace the contents of the structure *but keeps
the attributes of name intact*.  By analogy (or extension, whatever),

> t1[, c(1,2)] <- anything.at.all

will simply take the atomic part of anything.at.all and use it to replace
the contents of the first and second columns of the matrix structure t1, but
leaving all attributes (such as dimnames) intact.

This is not a bug but a feature.  There are times when you want to do this
and this is about the only elegant way for doing it.

Bill Venables, 
CMIS, CSIRO Marine Laboratories, 
PO Box 120, Cleveland, Qld. 4163
AUSTRALIA
Phone:  +61 7 3826 7251  
Fax:    +61 7 3826 7304
Mobile: +61 419 634 642
<mailto: Bill.Venables at csiro.au>
http://www.cmis.csiro.au/bill.venables/


>  -----Original Message-----
> From: 	Wiener, Matthew Charles [mailto:matthew_wiener at merck.com] 
> Sent:	Tuesday, 30 October 2001 11:40 PM
> To:	R-help
> Subject:	[R] moving dimnames when reassigning
> 
> 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
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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