lundi 1 octobre 2018

If else function for R

My dataframe is as follow:

'data.frame':   7 obs. of  3 variables:
 $ Currency : Factor w/ 3 levels "EUR","GBP","USD": 3 3 1 2 3 1 2
 $ Amount.LC: int  100 500 400 200 350 100 500
 $ FX.Rate  : Factor w/ 3 levels "0,89","1,201",..: 2 2 3 1 2 3 1

Based on an "if-else" function I tried to calculate a new column, called Amount.GC, depending whether a "EUR" is in column Currency or not. If "EUR" than I want to have the same value as in column Amount.LC. Else I want to have the division of Amount.LC and FX.Rate (Amount.LC/FX.Rate).

My code is as follow:

for (i in df$Currency){if (i == "EUR"){df$Amount.GC <- df$Amount.LC}
else{df$Amount.GC <- c(df$Amount.LC)/c(df$FX.Rate)}}

Unfortunately it doesn't work. Could someone please help me?

Aucun commentaire:

Enregistrer un commentaire