Mosi Ifatunji
2013-Sep-22 18:40 UTC
[R] Coding several dummy variables into a single categorical variable
Colleagues, I have generated several dummy variables: n$native0 <- 1 * (n$re=="white" & n$usborn=="yes") n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes") n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes") n$native3 <- 1 * (n$re=="carib" & n$usborn=="no") I would now like to combine these into a single categorical variable where the new variable would be n$native. And values of native would be 0 through 3, where n$native0 would be a 0 value on n$native, n$native1 would be a 1 value on n$native etc. Any help would be greatly appreciated. -- Mosi
arun
2013-Sep-22 19:25 UTC
[R] Coding several dummy variables into a single categorical variable
Hi, Try: set.seed(385) df<- data.frame(re= sample(c("white","afam","carib"),20,replace=TRUE), usborn= sample(c("yes","no"),20,replace=TRUE),stringsAsFactors=FALSE) df1<-within(df,{native3<- 1*(re=="carib" & usborn=="no"); native2<- 1*(re=="carib" & usborn=="yes"); native1<- 1*(re=="afam" & usborn=="yes"); native0<- 1*(re=="white" & usborn=="yes")}) library(reshape2) df2<- melt(df1,id.vars=c("re","usborn"))[,-3] colnames(df2)[3]<- "native" df2 A.K. ----- Original Message ----- From: Mosi Ifatunji <ifatunji at gmail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Sunday, September 22, 2013 2:40 PM Subject: [R] Coding several dummy variables into a single categorical variable Colleagues, I have generated several dummy variables: n$native0 <- 1 * (n$re=="white" & n$usborn=="yes") n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes") n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes") n$native3 <- 1 * (n$re=="carib" & n$usborn=="no") I would now like to combine these into a single categorical variable where the new variable would be n$native. And values of native would be 0 through 3, where n$native0 would be a 0 value on n$native, n$native1 would be a 1 value on n$native etc. Any help would be greatly appreciated. -- Mosi ______________________________________________ R-help at r-project.org mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Ista Zahn
2013-Sep-22 22:12 UTC
[R] Coding several dummy variables into a single categorical variable
Hi Mosi, The easiest approach would be be generate native from re and usborn directly, rather than generating dummy codes as an intermediate step: n$native <- interaction(n[c("re", "usborn")]) This has the advantage of preserving the meanings of the levels of native in the resulting factor, so that you don't have to remember what 0,1,2,3 mean. If for some reason you prefer the numeric levels you can then change them using the levels() function. If for some reason you don't have the original variables (re and usborn in your example), you can construct a factor from the dummy codes as follows: n$native <- factor( with(n, paste(native0, native1, native2, native3, sep = "")), levels = c("1000", "0100", "0010", "0001"), labels = c("0", "1", "2", "3")) Best, Ista On Sun, Sep 22, 2013 at 2:40 PM, Mosi Ifatunji <ifatunji at gmail.com> wrote:> Colleagues, > > I have generated several dummy variables: > > n$native0 <- 1 * (n$re=="white" & n$usborn=="yes") > n$native1 <- 1 * (n$re=="afam" & n$usborn=="yes") > n$native2 <- 1 * (n$re=="carib" & n$usborn=="yes") > n$native3 <- 1 * (n$re=="carib" & n$usborn=="no") > > I would now like to combine these into a single categorical variable where the new variable would be n$native. > > And values of native would be 0 through 3, where n$native0 would be a 0 value on n$native, n$native1 would be a 1 value on n$native etc. > > Any help would be greatly appreciated. > > -- Mosi > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.