jeudi 28 avril 2016

Nested IFELSE in adding a column to a data frame

I am trying to add a column to a data frame conditioned on values in existing column.

Data Frame:- df

Country

India
Mexico
Germany 

I am now adding the Continent Column based on the country value.

df$Continent <- ifelse(df$Country=="India","Asia","Europe")

Output:-

Country   Continent 
India       Asia
Mexico      Europe
Germany     Europe

This leads to Mexico being categorized as in Europe. How can I add more if statements or it would be helpful if someone can hint me an alternate method?

Aucun commentaire:

Enregistrer un commentaire