jeudi 2 avril 2020

How to use If function in R to create a column using multiple conditions

I am not familiar with R , I need your help for this issue , I have a data frame composed with 25 variables (25 columns) named df simplified

 name   experience      Club        age       Position 
 luc          2         FCB         18        Goalkeeper
 jean         9         Real        26        midfielder
 ronaldo      14        FCB         32        Goalkeeper
 jean         9         Real        26        midfielder
 messi        11        Liverpool   35        midfielder
 tevez        6         Chelsea     27        Attack
 inzaghi      9         Juve        34        Defender
 kwfni        17        Bayern      40        Attack
 Blabla       9         Real       25        midfielder
 wdfood      11        Liverpool   33        midfielder
 player2      7         Chelsea     28       Attack
 player3     10         Juve       34        Defender
 fgh         17        Bayern      40        Attack

I would like to add a column to this data frame named "country".This new column takes into account different conditions .

Juve      Italy
FCB       Spain
Real      Spain
Chelsea   England
Liverpool England
Bayern    Germany

So let say if the club is FCB or Real the value in country is Spain the output of df$Country should be as follows

Country
Spain 
Spain 
Spain 
Spain 
England
England 
Italy 
Germany
Spain 
England 
England
Italy 
Germany

The code I started to do is the following

df$country=ifelse(df$Club=="FCB","spain",    df$Club=="Real","Spain" ......)

But it seems false . knowing that my real data set has more than 250 different values in "club" column and more than 30 in "Country"

doing that manually seems too long .

Could you help me in that point please .

Aucun commentaire:

Enregistrer un commentaire