Home > database >  How to write a function that coverts temperatures in F, C, and K?
How to write a function that coverts temperatures in F, C, and K?

Time:09-12

I am attempting to create a function called temperatureConverter, which will be a one-stop shop for converting any temperature between F, C or K. Ultimately, it should work something like this:

newTemp=temperatureConverter(57,from="C",to="K")

I understand that several if-then-else statements are needed, and that I should have default values of "C" for from, and "F" for to. I just don't know how to code for this.

The following is what I have tried (see two attached screenshots). I think there's something wrong with my temperature converter function.

enter image description here enter image description here

CodePudding user response:

Next time, please paste in the actual code, rather than posting a picture of the code. All of your functions like cToF seem okay, but there's no need to assign the desired output to a variable, but if you do, then put that variable (c or whatever) on the last line of the function.

Your main problem is that you want something like

temperatureConverter <- function(input, from="C",to="K"){
  if (from == "C" & to =="K") {
    cTok(input)
  } else ...

Note the double == for checking equality inside the if.

CodePudding user response:

# Temperature converter

temp_convert <- function(x, from = "C", to = "F"){
    
    Celcius <- c('c', 'C', 'Celcius', 'celcius')
    Fahrenheit <- c('f', 'F', 'Fahrenheit', 'fahrenheit')
    Kelvin <- c('k', 'K', 'Kelvin', 'kelvin')
    all <- c(Celcius, Fahrenheit, Kelvin)
    
    # ------------------------ Handle exceptions -------------------
    if (!(from %in% all) | !(to %in% all)){
        x_ <- NA
        warning("Please key-in appropriate units, exp. C, F, K ...")
    }
    
    if (from == to){
        x_ <- x
        warning("Nothing change")
    }
    
    if (is.null(from) | is.null(to)){
        x_ <- x
        warning("Both the 'from' and 'to' args must be provided")
    }
    
    # ---------------------- Convert from Celcius ------------------
    if (from %in% Celcius){
        if (to %in% Fahrenheit)
            x_ <- x * (9 / 5)   32
        if (to %in% Kelvin)
            x_ <- x   273.15
    }
    
    # --------------------- Convert from Fahrenheit ----------------
    if (from %in% Fahrenheit){
        if (to %in% Celcius)
            x_ <- (x - 32) * (5 / 9)
        if (to %in% Kelvin)
            x_ <- (x - 32) * (5 / 9)   273.15
    }
    
    # ----------------------- Convert from Kelvin ------------------
    if (from %in% Kelvin){
        if (to %in% Celcius)
            x_ <- x - 273.15
        if (to %in% Kelvin)
            x_ <- (x - 273.15) * (9 / 5)   32
    }
    
    return(x_)
}

Console:

> temp_convert(20, from = "celcius", to = "F")
[1] 68

> temp_convert(20, from = "k", to = "c")
[1] -253.15

> temp_convert(20, from = "F", to = "F")
[1] 20
Warning message:
In temp_convert(20, from = "F", to = "F") : Nothing change

> temp_convert(20, from = "b", to = "F")
[1] NA
Warning message:
In temp_convert(20, from = "b", to = "F") :
  Please key-in appropriate units, exp. C, F, K ...
  • Related