R/convert_temperature.R

#' Kelvin to Celsius
#'
#' @param temp_kelvin temperature in Kelvin
#'
#' @return temperature in Celsius
#' @export
#'
#' @examples
#' kelvin_to_celsius(273.15)
kelvin_to_celsius <- function(temp_kelvin) {
  temp_celsius <- temp_kelvin - 273.15

  return (temp_celsius)
}

#' Celsius to Kelvin
#'
#' @param temp_celsius temperature in Celsius
#'
#' @return temperature in kelvin
#' @export
#'
#' @examples
#' celsius_to_kelvin(89)
celsius_to_kelvin <- function(temp_celsius) {
  temp_kelvin <- temp_celsius + 273.15

  return (temp_kelvin)
}

#' Fahrenheit to Celsius
#'
#' @param temp_fahrenheit temperature in Fahrenheit
#'
#' @return temperature in Celsius
#' @export
#'
#' @examples
#' fahrenheit_to_celsius(56)
fahrenheit_to_celsius <- function(temp_fahrenheit) {
  temp_celsius <- (temp_fahrenheit - 32) * (5/9)

  return (temp_celsius)
}



#' Celsius to Fahrenheit
#'
#' @param temp_celsius temperature in Celsius
#'
#' @return temperature in Fahrenheit
#' @export
#'
#' @examples
#' celsius_to_fahrenheit(64)
celsius_to_fahrenheit <- function(temp_celsius) {
  temp_fahrenheit <- (temp_celsius * 9/5) + 32

  return (temp_fahrenheit)
}


#' Fahrenheit to Kelvin
#'
#' @param temp_fahrenheit temperature in Fahrenheit
#'
#' @return temperature in Kelvin
#' @export
#'
#' @examples
#' fahrenheit_to_kelvin(123)
fahrenheit_to_kelvin <- function(temp_fahrenheit) {
  temp_kelvin <- (temp_fahrenheit + 459.67) * 5/9

  return (temp_kelvin)
}


#' Kelvin to Fahrenheit
#'
#' @param temp_kelvin temperature in Kelvin
#'
#' @return temperature in Fahrenheit
#' @export
#'
#' @examples
#' kelvin_to_fahrenheit(89)
kelvin_to_fahrenheit <- function(temp_kelvin) {
  temp_fahrenheit <- (temp_kelvin * 9/5) - 459.67

  return (temp_fahrenheit)
}



#' Convert Temperature
#'
#' @param temp temperature value
#' @param from convert from unit
#' @param to convert to unit
#'
#' @return temperature
#' @export
#'
#' @examples
#' convert_temperature(0, from="celsius", to="kelvin")
convert_temperature <- function(temp, from, to) {
  if(from == "celsius" && to == "kelvin") {
    celsius_to_kelvin(temp)
  } else if (from == "kelvin" && to == "celsius") {
    kelvin_to_celsius(temp)
  } else if (from == "fahrenheit" && to == "celsius") {
    fahrenheit_to_celsius(temp)
  } else if (from == "celsius" && to == "fahrenheit") {
    celsius_to_fahrenheit(temp)
  } else if (from == "fahrenheit" && to == "kelvin") {
    fahrenheit_to_kelvin(temp)
  } else if (from == "kelvin" && to == "fahrenheit") {
    kelvin_to_fahrenheit(temp)
  } else {
    return("Incorrect temperature units.")
  }
}
scarecrow21/convertr documentation built on June 8, 2019, 3:46 a.m.