Quantcast
Channel: ListenData
Viewing all articles
Browse latest Browse all 425

Create Dummy Columns From Categorical Variable

$
0
0
The following code returns new dummy columns from a categorical variable.
Create dummy columns in R
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)}

Viewing all articles
Browse latest Browse all 425

Trending Articles