mercredi 20 février 2019

If value in a column starts with...mutate another column with given text, in R

I'm trying to build an if function that allows me to mutate the "city" column of a dataframe with a certain city name if in the "zipcode" column the value starts with a certain number.

For example: If zipcode starts with 1, mutate city column value with "NYC", else if zipcode starts with 6, mutate city column value with "Chicago", else if zipcode starts with 2, mutate city column value with "Boston",

and so on.

Dataframe name data.frame Column name zipcode and city.

I want do directly mutate the dataframe as I will need it for further use.

PS: Sorry for bad writing. I'm new in the community.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire