Create Dummy Columns From Categorical Variable


R Data Science: R Programming A-Z: R For Data Science With Real Exercises!

The following code returns new dummy columns from a categorical variable.
DF <- data.frame(strcol = c("A", "A", "B", "F", "C", "G", "C", "D", "E", "F"))
for(level in unique(DF$strcol)){
DF[paste("strcol", level, sep = "_")] <- ifelse(DF$strcol == level, 1, 0)}
Coursera Data Science

R Tutorials : 75 Free R Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

2 Responses to "Create Dummy Columns From Categorical Variable"

  1. Is there a faster data.table way to do that?

    ReplyDelete
  2. Is there a faster data.table way to do that?

    ReplyDelete

Next → ← Prev