Skip to content

To transform a vector of qualitatives values into a dataframe of quantitatives values

4 messages · Arnaud Michel, PIKAL Petr, Duncan Murdoch +1 more

#
Hi

 From the vector
X <- c(A, A, B, C, B, A, C)

I would like to build the Dataframe :
data.frame( A=c(1,1,0,0,0,1,0), B=c(0,0,1,0,1,0,0), C=c(0,0,0,1,0,0,1))

Any ideas ?
#
Hi
What is A, B and C? If you expect them to be letters, they need to be in parentheses.
X <- sample(letters[1:3], 10, replace=T)
X
 [1] "c" "c" "a" "b" "c" "c" "a" "a" "a" "a"
data.frame(A=(X=="a"), B=(X=="b"), C=(X=="c"))

Petr
#
On 13-12-11 8:22 AM, PIKAL Petr wrote:
You mean "quotes", not parentheses.
A simpler way is to use model.matrix.  With your example,

X <- factor(X)
m <- model.matrix( ~ X - 1)

(The names of the columns may need adjusting.)

Duncan Murdoch