I think this question is related to dummy-coding, but I am not exactly sure and don't know how to phrase it, so bear with me.
There is a data.frame with a numerical variable, which can assume values c(1:8) Now, instead I would like to have an extra column containing each possible value and a second column that contains one/zero to indicate the presence of that value. The follwing code should give you an idea.
#reproducible code
set.seed(1)
exdf <- expand.grid(id=c(1:10), content=c(1:2))
exdf$dv <- sample(c(1:8), size=20, replace=TRUE)
#now this should approximately turn into the following,
#however the column "dv_value" should contain 0 or 1, depending on what number was in exdf$dv
desired <- expand.grid(id=c(1:10), content=c(1:2), dv_label=unique(exdf$dv), dv_value=NA)
How can I do this?
Well, the linked duplicate is not really a duplicate, but one of the answers there gave me lead. So this is what I came up with.
#solution
exdf$dummy1 <- as.numeric(exdf$dv == 1)
exdf$dummy2 <- as.numeric(exdf$dv == 2)
exdf$dummy3 <- as.numeric(exdf$dv == 3)
exdf$dummy4 <- as.numeric(exdf$dv == 4)
exdf$dummy5 <- as.numeric(exdf$dv == 5)
exdf$dummy6 <- as.numeric(exdf$dv == 6)
exdf$dummy7 <- as.numeric(exdf$dv == 7)
exdf$dummy8 <- as.numeric(exdf$dv == 8)
library(reshape2)
exdf2 <- melt(data=exdf, id.vars=c("id", "content", "dv"), variable.name="dv_label", value.name="dv_value")