Re: [S] create dummy variables

Jan Schelling (schelling@ivuk.mavt.ethz.ch)
Thu, 25 Jun 1998 23:12:00 +0200


Jane,

the function col( ) does the trick:

f <- c(0, 2, 1, 2, 0)
m <- matrix(0, nrow = length(f), ncol = max(f))
m[col(m) <= f ] <- 1
data.frame(m)

X1 X2
1 0 0
2 1 1
3 1 0
4 1 1
5 0 0

Jan
-----------------------------------------------------------------
Jan Schelling
Institute of Process Engineering
ETH Zurich, 8092 Switzerland
phone: ++41-1-633 62 63
fax: ++41-1-633 11 19
email: schelling@ivuk.mavt.ethz.ch
internet: http://www.ivuk.ethz.ch/staff/schelling/
-----------------------------------------------------------------

At 15:47 25.06.98 -0400, Jing (Jane) Xu wrote:
>
>Hi, everyone,
>
>Is there a simple way to create (n-1) dummy variables from a single
>variable which takes values 0,1,...,n-1?
>
>Say, I have a variable f like:
> f
> 0
> 2
> 1
> 2
> 0
>how can I get something like:
> d1 d2
> 0 0
> 1 1
> 1 0
> 1 1
> 0 0
>
>Any help would be greatly appreciated!
>
>Jane
>
>
>-----------------------------------------------------------------------
>This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
>send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
>message: unsubscribe s-news
>
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news