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!
|