Is there a way to create a function in r which takes in the value to convert, the input weight unit and output weight unit?
converter<-fuction(value_in, unit_in, unit_out){
#some code
return value_out
}
For example i want to convert 1 kg to grams. So i would write Something like this:
if (value_in=="kg" and value_out=="grams"){
return (value/1000)
}
But how do i write this if i have like over ten weight units (for example kg, st, ls, oz etc.). Do i have to write an if else statement for every possible opportunity or is there a simpler way? I thought about the switch statement but not sure how to use two expressions here.