BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi,

I have no idea of R-programming, I want to understand this script in SAS. Could you help me out.

 

#--------------------------------------------------------------------------#

dt3 <- dt2 %>%

mutate(Race_Ethnicity = case_when(

   FirstRace %in% c('American Indian/Black', 'Black', 'Black/White') ~ 'Black'

   ,Ethnicity == 'Hispanic or Latino' & !(FirstRace %in% c('American Indian/Black', 'Black', 'Black/White')) ~ 'HispanicLatino'

   ,FirstRace %in% c('White', 'Middle Eastern') & Ethnicity != 'Hispanic or Latino' ~ 'White'

   ,FirstRace %in% c("Asian", "Oriental", "American Indian/Asian", "Asian/White", "American Indian/White") & Ethnicity != 'Hispanic or Latino' ~ "Asian"

   ,FirstRace %in% c("*Unknown", "American Indian/Alaska Native","Decline", "Do Not Know", "Multi-Racial" ,

                     "Native Hawaiian/Pacific Islande", "Other", "Z") & Ethnicity != 'Hispanic or Latino' ~ 'Other_Unknown'

   ,TRUE ~ "Other_Unknown" #some missing value

   ))

dt3$FirstRace[dt3$FirstRace %in% c("*Unknown", "Do Not Know", "Decline")] <- "Unknown"

 

 

thanks

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Do you know what is this script trying to do? (You must have some idea, or you wouldn't be trying to make use of it)

--
Paige Miller
Reeza
Super User

It creates a new data set, dt3 from a data set dt2, with a new variable Race_Ethnicity based on the variables of First_Race and Ethnicity variable.

 

dt3 <- dt2 %>%

mutate(Race_Ethnicity = case_when(

   FirstRace %in% c('American Indian/Black', 'Black', 'Black/White') ~ 'Black'

   ,Ethnicity == 'Hispanic or Latino' & !(FirstRace %in% c('American Indian/Black', 'Black', 'Black/White')) ~ 'HispanicLatino'

   ,FirstRace %in% c('White', 'Middle Eastern') & Ethnicity != 'Hispanic or Latino' ~ 'White'

   ,FirstRace %in% c("Asian", "Oriental", "American Indian/Asian", "Asian/White", "American Indian/White") & Ethnicity != 'Hispanic or Latino' ~ "Asian"

   ,FirstRace %in% c("*Unknown", "American Indian/Alaska Native","Decline", "Do Not Know", "Multi-Racial" ,

                     "Native Hawaiian/Pacific Islande", "Other", "Z") & Ethnicity != 'Hispanic or Latino' ~ 'Other_Unknown'

   ,TRUE ~ "Other_Unknown" #some missing value

   ))

e.g American/Indian/Black, Black, Black/White are coded to black

White, Middle Eastern & Ethnicity ne Hispanic or Latino is coded to white. 

I think you should be able to deduce the remaining categories. 

 

 

dt3$FirstRace[dt3$FirstRace %in% c("*Unknown", "Do Not Know", "Decline")] <- "Unknown"

It then maps the values of the First Race to unknown in the DT3 dataset. 

 

It's basically an IF/THEN series of statements.