I have a large dataset and need to recode a few numeric variables to other numeric values. A portion of my dataset looks like this:
condition.10 financial.condition behavior.condition outcome Gender Race
1 6 1 3 0 Male 5
2 7 0 4 0 Female 5
3 5 0 3 1 Female 5
4 2 1 1 1 Male 2,5
5 10 1 5 0 Female 5
6 6 1 3 1 Male 5
I want to recode race into 3 "bins" of 1, 2, 3- "white", "Black", and "Other" respectively. I have managed to achieve that with this code:
mydata$Race <- NA
mydata$Race <- mydata$Q73
mydata$Race[mydata$Race==1|mydata$Race==2|mydata$Race==4|mydata$Race==6]<-6
mydata$Race[mydata$Race==3]<-2
mydata$Race[mydata$Race==5]<-1
mydata$Race[mydata$Race==6]<-3
I also tried this:
case_when(mydata$Race %in% c(1,2,4,6) ~3,
mydata$Race %in% 3 ~ 2,
mydata$Race %in% 5 ~1,
TRUE ~ as.numeric(mydata$Race))
The first bit gives me what I need, but it doesn't account for people checking two races such as that in the 4th row.
Any advice would be appreciated. I already ready recode from car package and dplyr.
Maybe it just me being so new, but it hurts not being able to do the basics.
unique(mydata$Race)
# [1] 5 2,5 2 3 6 3,5 1,5 1,2,4,5 1 1,2,5 4,6 3,6 2,3 1,3 4
# [16] 2,4,5,6 1,3,5 4,5
# Levels: 1 1,2,4,5 1,2,5 1,3 1,3,5 1,5 2 2,3 2,4,5,6 2,5 3 3,5 3,6 4 4,5 4,6 5 6
Note: I am very new to R and am looking for some guidance.
2, 5as 1 or 3? - zx8754unique(mydata$Race)? - zx8754unique(mydata$Race) [1] 5 2,5 2 3 6 3,5 1,5 1,2,4,5 1 1,2,5 4,6 3,6 2,3 1,3 4 [16] 2,4,5,6 1,3,5 4,5 Levels: 1 1,2,4,5 1,2,5 1,3 1,3,5 1,5 2 2,3 2,4,5,6 2,5 3 3,5 3,6 4 4,5 4,6 5 6I'm not sure how to make this a more readable - J Bacon