Reputation: 305
Any help with this would be much appreciated.
I have four overlapping race variables that I would like to make mutually exclusive and code the remainder into a new variable mixed race variable. I've been trying to use if and ifelse and failing miserably.
white<-c(1,1,1,NA)
black<-c(0,NA,1,0)
asian<-c(0,0,0,0)
aian<- c(0,0,0,0)
white.n<-c(1,1,0,NA)
mix<-c(0,0,1,0)
df<-cbind(white,black,asian,aian,white.n,mix)
df
white black asian aian white.n mix
[1,] 1 0 0 0 1 0
[2,] 1 NA 0 0 1 0
[3,] 1 1 0 0 0 1
[4,] NA 0 0 0 NA 0
Any thoughts would be much appreciated.
Upvotes: 2
Views: 182
Reputation: 149
You want a new column that codes everything into a mutually exclusive variable, right? Like this?
white<-c(1,1,1,NA,0)
black<-c(0,NA,1,0,0)
asian<-c(0,0,0,0,1)
white<-c(1,1,0,NA,0)
df<-data.frame(white,black,asian,white)
df
row.count <- rowSums(df, na.rm=T)
df$code[row.count > 1] <- "Mixed"
df$code[row.count == 1] <- names(which.max(df[row.count == 1, ]))
Upvotes: 1