[R] Found "answer" to my question on MTB's INDICATOR

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Dec 22 10:31:40 CET 2000


"C. Joseph Lu" <cjlu at ibm.stat.ncku.edu.tw> writes:

> Dear R-Friends,
> 
> Sorry to bother everyone with my earlier question:
>     "Do we have similar R function to work like Minitab's INDICATOR?"
> 
> I found the way to make things work: e.g.,
> > x <- c(2,2,5,3,6,5,NA)
> > model.matrix(~ factor(x) - 1)
>   factor(x)2 factor(x)3 factor(x)5 factor(x)6
> 1          1          0          0          0
> 2          1          0          0          0
> 3          0          0          1          0
> 4          0          1          0          0
> 5          0          0          0          1
> 6          0          0          1          0
> attr(,"assign")
> [1] 1 1 1 1
> 
> NA does not appear, fine with me.
> 
> Certainly, this is not exactly the same as Minitab's INDICATOR:
> 
> INDICATOR  C2, C11-C15
> 
>     C2          C11 C12 C13 C14 C15
>     2             1   0   0   0   0
>     2             1   0   0   0   0
>     5 ------>     0   0   0   1   0
>     3             0   1   0   0   0
>     6             0   0   0   0   1
>     5             0   0   0   1   0
>     *             *   *   *   *   *
> where * is missing code in Minitab.
> 
> Certainly, I still welcome any suggestion that you might have.

Looks like Minitab is creating a column of zeroes for the non-existing
level "4". You can get that with

> x <- c(2,2,5,3,6,5,NA)
> xf <- factor(x,levels=2:6)
> model.matrix(~xf-1)
  xf2 xf3 xf4 xf5 xf6
1   1   0   0   0   0
2   1   0   0   0   0
3   0   0   0   1   0
4   0   1   0   0   0
5   0   0   0   0   1
6   0   0   0   1   0
attr(,"assign")
[1] 1 1 1 1 1


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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