Я довольно новичок в R, и я в настоящее время играю с функциями, и если операторы
, поэтому я сделал data.frame следующим образом:
cash=c(50, 480, 45, 1000)
currency=c("chf", "eur", "gbp", "twd")
cash_tot=data.frame(cash, currency)
cash_tot
и яЯ хотел бы создать своего рода функцию «конвертации наличных» только для валют, совпадающих с валютами CHF и EUR.Поэтому я попробовал следующий код:
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)
Все, что я получу взамен, это
[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
кто-нибудь, кто мог бы помочь?Заранее прошу прощения, если это глупый вопрос, ахах:)