convert_temperature <- function(value, from_unit, to_unit) {
if (from_unit == "C" & to_unit == "F") {
return((value * 9/5) + 32)
} else if (from_unit == "F" & to_unit == "C") {
return((value - 32) * 5/9)
} else if (from_unit == "C" & to_unit == "K") {
return(value + 273.15)
} else if (from_unit == "K" & to_unit == "C") {
return(value - 273.15)
} else if (from_unit == "F" & to_unit == "K") {
return((value - 32) * 5/9 + 273.15)
} else if (from_unit == "K" & to_unit == "F") {
return((value - 273.15) * 9/5 + 32)
} else {
return("Invalid input! Please use 'C', 'F', or 'K' as units.")
}
}
# Example usage:
convert_temperature(100, "C", "F") # Converts 100°C to Fahrenheit
## [1] 212
convert_temperature(32, "F", "C") # Converts 32°F to Celsius
## [1] 0
convert_temperature(0, "C", "K") # Converts 0°C to Kelvin
## [1] 273.15