lundi 15 avril 2019

How can I combine two columns in R when the variable are not numerical?

I have two columns in a data table in R. One is race and the other is ethnicity. I would like to combine these two columns to make one new column labeled R/E. However, I am having difficulty combining the variables.

Let's say I have these two columns.

Ethnicity : Hispanic or Latino, Not Hispanic or Latino, Not Hispanic or Latino, Unknown, Unknown

Race: Unknown, Asian, Unknown, Asian, Unknown


Desired Column (R/E) : Hispanic or Latino, Asian, Not Hispanic or Latino, Asian, Unknown

I want to create one column where I can combine these variables accordingly. I have tried to use ifelse but I am not sure of the correct syntax.

I basically want to say: If ethnicity = "Hispanic or Latino" -> R/E = "Hispanic or Latino", If ethnicity = "Unknown" or ethnicity = "Not Hispanic or Latino", then check "Race"

Can I use ifelse to do this? Or is there another function I ca use to achieve this?

I am not sure where to go from NewData$R/E <-ifelse(NewData$Etn<= Hispanic or Laino, "Hispanic or Latino", ifelse(NewData$Etn<= Unknown,

Aucun commentaire:

Enregistrer un commentaire