lundi 26 novembre 2018

Currency converter function, if statement not matching names

I'm fairly a newbie with R and i'm currently playing around with functions and if statements

so i made a data.frame as follows:

cash=c(50, 480, 45, 1000)
currency=c("chf", "eur", "gbp", "twd")
cash_tot=data.frame(cash, currency)
cash_tot

and i'd like to create a sort of "cash converting" function for cash matching chf and eur currencies only. So i tried the following code:

converter=function(x){
 for(currency in x){
   if (currency=="chf"){
   return(cash)
}
else if (currency=="eur"){
  return(cash/1.13)
}
  else{
  print("Calculate it yourself :)")
  }
  }
}

converter(cash_tot)

All i get in return is

[1] "Calculate it yourself :)"
Warning messages:
1: In if (currency == "chf") { :the condition has length > 1 and only the first element will be used
2: In if (currency == "eur") { :the condition has length > 1 and only the first element will be used
3: In if (currency == "chf") { :the condition has length > 1 and only the first element will be used

anyone who might be able to help? I apologize in advance if it's a stupid question ahah :)

Aucun commentaire:

Enregistrer un commentaire