s-news
[Top] [All Lists]

Convert variable to dummies

To: s-news@lists.biostat.wustl.edu
Subject: Convert variable to dummies
From: Stuart Luppescu <s-luppescu@uchicago.edu>
Date: Tue, 24 Oct 2006 12:15:19 -0500
User-agent: Thunderbird 1.5.0.7 (X11/20061003)
Hello, I want to convert a variable x that has values {0, 1, 2, 3, 4} into four dummies such that dummy1 is 1 if x has the value 1, dummy2 is 1 if x has the value 2, etc. I made an n x 4 matrix of zeros and then tried to do the.matrix[,x] <- 1, but this makes all rows and columns 1. I can do it in a for loop like this, I suppose:
for(i in 1:length(x)) the.matrix[i,x] <- x[i]
but this seems very clumsy and un-S-like. Is there an elegant way to do this that takes advantage of the vectorization in S?

Thanks.

--
Stuart Luppescu -=-=- s-luppescu <AT> uchicago <DOT> edu
CCSR at U of C ,.;-*^*-;.,  ccsr.uchicago.edu
    (^_^)/    才文と智奈美の父
Thank God I'm an atheist!


<Prev in Thread] Current Thread [Next in Thread>
  • Convert variable to dummies, Stuart Luppescu <=