s-news
[Top] [All Lists]

Re: [S] create dummy variables

To: "Jing (Jane) Xu" <jxu@athena.biostat.jhsph.edu>, s-news@wubios.wustl.edu
Subject: Re: [S] create dummy variables
From: "Jan Schelling" <schelling@ivuk.mavt.ethz.ch>
Date: Thu, 25 Jun 1998 23:12:00 +0200
In-reply-to: <Pine.GSO.3.96.980625154152.3822A-100000@athena.biostat.jhs ph.edu>
References: <6rbtrhi89p.fsf@verdi.stat.wisc.edu>
Sender: owner-s-news@wubios.wustl.edu
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

<Prev in Thread] Current Thread [Next in Thread>