[R] Coding several dummy variables into a single categorical variable

arun smartpink111 at yahoo.com
Sun Sep 22 21:45:09 CEST 2013


Hi,

If you don't want to install any package:
colnames(df1)[grep("native",colnames(df1))]<- gsub("([[:alpha:]])(\\d+)","\\1_\\2",colnames(df1)[grep("native",colnames(df1))])
  df2<-reshape(df1,direction="long",varying=3:ncol(df1),sep="_")[,-5]
df3<-df2[as.logical(df2$native),-4]
colnames(df3)[3]<- "native"
 row.names(df3)<- 1:nrow(df3)
 identical(df2New,df3)
#[1] TRUE
A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Mosi Ifatunji <ifatunji at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Sunday, September 22, 2013 3:34 PM
Subject: Re: [R] Coding several dummy variables into a single categorical	variable

Hi,

Not sure this is what you wanted.
df2<- melt(df1,id.vars=c("re","usborn"))
df2New<-df2[df2$value==1,-4]
 df2New$variable<-as.numeric(gsub("[[:alpha:]]","",df2New$variable))
colnames(df2New)[3]<- "native"
 row.names(df2New)<- 1:nrow(df2New)
df2New
#      re usborn native
#1  white    yes      0
#2  white    yes      0
#3  white    yes      0
#4  white    yes      0
#5  white    yes      0
#6   afam    yes      1
#7   afam    yes      1
#8   afam    yes      1
#9  carib    yes      2
#10 carib    yes      2
#11 carib    yes      2
#12 carib    yes      2
#13 carib    yes      2
#14 carib     no      3
#15 carib     no      3
#16 carib     no      3
#17 carib     no      3
#18 carib     no      3


A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Mosi Ifatunji <ifatunji at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Sunday, September 22, 2013 3:25 PM
Subject: Re: [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"



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
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list