Message-ID: <BAD2EBD5-1D16-4A40-8C63-5263945A5FC3@eawag.ch>
Date: 2009-03-07T13:39:06Z
From: soeren.vogel at eawag.ch
Subject: Recode factor into binary factor-level vars
How to I "recode" a factor into a binary data frame according to the
factor levels:
### example:start
set.seed(20)
l <- sample(rep.int(c("locA", "locB", "locC", "locD"), 100), 10,
replace=T)
# [1] "locD" "locD" "locD" "locD" "locB" "locA" "locA" "locA" "locD"
"locA"
### example:end
What I want in the end is the following:
m$locA: 0, 0, 0, 0, 0, 1, 1, 1, 0, 1
m$locB: 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
m$locC: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
m$locD: 1, 1, 1, 1, 0, 0, 0, 0, 1, 0
Instead of 0, NA's would also be fine.
Thanks, S?ren
--
S?ren Vogel, PhD-Student, Eawag, Dept. SIAM
http://www.eawag.ch, http://sozmod.eawag.ch