I want to convert a discrete (identifier) ββvariable into a series of logical columns so that I can use this variable as a function of the logistic regression function (and others) where I cannot mix continuous and discrete values.
I have a factor column in a data frame, and I want to convert the column to a column matrix (1 .. "number of levels") of logical values, for example:
my_labels=c("a","b","c","d","e","f")
my_tally=c(1,1,3,2,3,4,5,1)
my_tally=factor(my_tally, levels=c(1:6), labels=my_labels)
summary(my_tally)
expected_output=c(1,0,0,0,0,0,
1,0,0,0,0,0,
0,0,1,0,0,0,
0,1,0,0,0,0,
0,0,1,0,0,0,
0,0,0,1,0,0,
0,0,0,0,1,0,
1,0,0,0,0,0
)
expected_output=matrix(expected_output,
nrow=length(my_tally),
ncol=length(levels(my_tally)),
byrow=TRUE
)
expected_output
colSums(expected_output)
Any suggestions for a "quick" function to create expect_output? This is a big data problem (700 discrete capabilities, 1M observations).