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
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
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
3 matches
Mail list logo