I am trying to replace the values in a column of a data.table based on a series of conditions in another data.table. For example, if I had the following values in a column:
dt = data.table("cand"=c("Trump, Donald","Clinton, Hillary","Rubio, Marco","Pelosi, Nancy"))
And I had another data.table that looked like this:
dt.comp = data.table("cand"=c("Trump, Donald","Clinton, Hillary","Rubio, Marco","Pelosi, Nancy"),
"party"=c("REP","DEM","REP","DEM"), "office"=c("Pres","Pres","Sen","House"))
And I wanted the original column to look like this:
dt.ideal=data.table("cand"=c("rep.pres","dem.pres","rep.sen","dem.house"))
How would I go about doing that? I've tried a series of nested ifelse() functions that start like so:
dt.ideal <- ifelse(dt %in% dt.comp$cand & dt.comp$party[which(dt==dt.comp$cand)]=="DEM" & dt.comp$office[which(dt==dt.comp$cand)]=="House",
"dem.house.cand",
But when I spell out all the various conditions, I get an warning that says: longer object length is not a multiple of shorter object length
I feel like I'm close to the solution, but I just can't get to it. Thanks in advance for your help!
Aucun commentaire:
Enregistrer un commentaire