[R] What is "assign" attribute?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Feb 25 13:31:51 CET 2010


rkevinburton at charter.net wrote:
> I am just curious. Every once and a while I see an attribute attached to an object called "assign". What meaning does this have? For example:
> 
> dist ~ speed, data=cars
> 
> forms a matrix like:
> 
>  num [1:50, 1:2] 1 1 1 1 1 1 1 1 1 1 ...
>  - attr(*, "dimnames")=List of 2
>   ..$ : chr [1:50] "1" "2" "3" "4" ...
>   ..$ : chr [1:2] "(Intercept)" "speed"
>  - attr(*, "assign")= int [1:2] 0 1
> 
> The "dimnames" attribute is fairly self-explanatory. I just am not sure what the "assign" attribute means.

It has to do with the mapping between terms of the formula and columns 
of the design matrix:

 > str(model.matrix(Ozone~factor(Month)+Wind,data=airquality))
  num [1:116, 1:6] 1 1 1 1 1 1 1 1 1 1 ...
  - attr(*, "dimnames")=List of 2
   ..$ : chr [1:116] "1" "2" "3" "4" ...
   ..$ : chr [1:6] "(Intercept)" "factor(Month)6" "factor(Month)7" 
"factor(Month)8" ...
  - attr(*, "assign")= int [1:6] 0 1 1 1 1 2
  - attr(*, "contrasts")=List of 1
   ..$ factor(Month): chr "contr.treatment"

I.e. columns 2:5 belong to the first non-intercept term, factor(Month). 
(Notice that it is implicitly assumed that you have the corresponding 
terms() output to hand, likewise for the "contrasts" attribute.)
-- 
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907



More information about the R-help mailing list