# [R] Do we have similar function to Minitab's INDICATOR?

Bill.Venables@CMIS.CSIRO.AU Bill.Venables at CMIS.CSIRO.AU
Sat Dec 23 02:21:27 CET 2000

```Thomas Lumley writes:

> -----Original Message-----
> From: Thomas Lumley [mailto:thomas at biostat.washington.edu]
> Sent: Saturday, 23 December 2000 3:18
> To: C. Joseph Lu
> Cc: 'r-help'
> Subject: Re: [R] Do we have similar function to Minitab's INDICATOR?
>
>
> On Fri, 22 Dec 2000, C. Joseph Lu wrote:
>
> > Dear R-Friends,
> >
> > Do we have a similar R function to work like Minitab's INDICATOR?
>
> You can do it with
>
> outer(C2,sort(unique(C2)),function(i,j) as.numeric(i=j))
>

BZZZzzzt! You meant to say     as.numeric(i == j)

There are a number of ways of doing this in a fairly slick manner.  The one
above is the neatest that I know of, but the following one is also pretty
good and quite instructive.

v <- sort(unique(c2))
X <- matrix(0, length(c2), length(v))
X[cbind(1:length(c2), match(c2, v))] <- 1

The obvious one, X <- model.matrix(~ factor(c2) - 1), is what most people
would use in a one-off situation, I suppose, but for programming it would be
much less efficient than either of those above.

(I mention all this with some diffidence, though.  The number of times you
really need to generate an indicator matrix explicitly yourself is pretty
small.  You can usually achieve the same ultimate result by better means.)

Bill Venables

> and model.matrix also does something similar.
>
>
> 	-thomas
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```