Re: [R] Recode factor into binary factor-level vars

2009-03-07 Thread David Winsemius
Sören; You need to somehow add back to the information that is in "l" that fact that it was sampled from a set with 4 elements. Since you didn't sample from a factor the level information was lost. Otherwise, you coud create that list with unique(l) which in this case only returns 3 eleme

Re: [R] Recode factor into binary factor-level vars

2009-03-07 Thread Dimitris Rizopoulos
one way is: set.seed(20) l <- sample(rep.int(c("locA", "locB", "locC", "locD"), 100), 10, replace=T) f <- factor(l, levels = paste("loc", LETTERS[1:4], sep = "")) m <- as.data.frame(model.matrix(~ f - 1)) names(m) <- levels(f) m I hope it helps. Best, Dimitris soeren.vo...@eawag.ch wrote: H

[R] Recode factor into binary factor-level vars

2009-03-07 Thread soeren . vogel
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