简体   繁体   中英

Recode column in R

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.

We can create a lookup table with the Race codes you're looking for. Anything not in that table, we can call "Other".

library(tidyverse)

#create a lookup table
RaceTable <- data.frame(Race = c(3, 5),
                        RaceName = c("White", "Black"),
                        stringsAsFactors = FALSE)

mydata %>% 
  #bring in RaceName from the lookup table
  left_join(RaceTable, by = c("Race" = "Race")) %>% 
  #if there is no RaceName, call it "Other"
  mutate(RaceName = replace(RaceName, is.na(RaceName), "Other"))

We can create a lookup named vector then loop through values:

# example data
df1 <- data.frame(Race = c("1", "2", "3", "4", "5", "5,2", "6"))

# map, named vector
lookup <- setNames(c(3, 3, 2, 3, 1, 3), 1:6)
# 1 2 3 4 5 6 
# 3 3 2 3 1 3 

df1$RaceClean <- sapply(as.character(df1$Race), function(i){
  paste(lookup[ unlist(strsplit(i, ",")) ], collapse = ",")
  })

df1
#   Race RaceClean
# 1    1         3
# 2    2         3
# 3    3         2
# 4    4         3
# 5    5         1
# 6  5,2       1,3
# 7    6         3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM