0

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")
vanao veneri
  • 970
  • 2
  • 12
  • 31
  • 2
    Is there a specific reason you need this? Many modeling functions in R are comfortable doing this for you if the column is a factor using the default contrast. You cam look into `model.frame` if you really needs to do this in some other context. – MrFlick Sep 12 '16 at 13:47
  • @MrFlick, yes there is. Kind of hard to explain, but I need this to create further variables that I inted to run analyses with. I would like to merge this with another data.frame that has a structure like this. – vanao veneri Sep 12 '16 at 14:02

0 Answers0