mercredi 27 septembre 2017

Mutate and ifelse () basic coding?

I'm trying to mutate the revised dataframe by adding a new column called Residency.

The values in Residency should be computed as follows, using the values in DriverState: Maryland(MD) -> "local"; neighboring states such as Pennsylvania (PA), Virgina (VA), West Virginia (WV), Delaware (DE), and New Jersey (NJ) -> "neighbor"; other states -> "other".

I'm trying to use mutate() and ifelse(), but I can't seem to code it correctly. Please see below:

dataset.obj.revised <- mutate(dataset.obj, Redisdency = as.factor(ifelse(dataset.obj$DriverState == "MD", "local", > ifelse(dataset.obj$DriverState == "Other", "other", > "neighbor"))))

I would end up getting this instead:

Residency
local :1049526
neighbor: 112202
NA's : 11

Aucun commentaire:

Enregistrer un commentaire