R/convert.R

Defines functions fat_to_kcal kcal_to_joule kcal_to_protein kcal_to_fat kcal_to_carbs fluidounce_to_liter inch_to_cm lbs_to_kg

Documented in fat_to_kcal fluidounce_to_liter inch_to_cm kcal_to_carbs kcal_to_fat kcal_to_joule kcal_to_protein lbs_to_kg

#' @title Lbs to kilograms
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert lbs to kilogram.
#'
#' @param lbs weight in lbs
#'
#' @return Weight in kilograms
#'
#' @examples
#' lbs_to_kg(150)
#' @rdname lbs_to_kg
#' @export
lbs_to_kg <- function(lbs) {
  checkmate::assert_number(lbs, lower = 0)
  kg <- lbs * 0.45359237
  return(kg)
}

#' @title Inches to centimeters
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert inch to centimeter.
#'
#' @param inch length in inches
#'
#' @return Length in centimeters
#'
#' @examples
#' inch_to_cm(inch = 70)
#' @rdname inch_to_cm
#' @export
inch_to_cm <- function(inch) {
  checkmate::assert_number(inch, lower = 0)
  cm <- inch * 2.54
  return(cm)
}

#' @title Fluid ounces to liters
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert fluid ounce to liter.
#'
#' @param fluid_ounces fluid ounces
#'
#' @return Volume in liter
#'
#' @examples
#' fluidounce_to_liter(70)
#' @rdname fluidounce_to_liter
#' @export
fluidounce_to_liter <- function(fluid_ounces) {
  checkmate::assert_number(fluid_ounces, lower = 0)
  liters <- fluid_ounces / 33.8
  return(liters)
}

#' @title Kcal to carbs
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert kcal to carbohydrates in gram
#'
#' @param kcal kilocalories
#'
#' @return grams of carbohydrates
#'
#' @examples
#' kcal_to_carbs(100)
#' @rdname kcal_to_carbs
#' @export
kcal_to_carbs <- function(kcal) {
  checkmate::assert_number(kcal, lower = 0)
  carbs <- kcal * 0.25
  return(carbs)
}

#' @title Kcal to fat
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert kcal to fat in gram
#'
#' @param kcal kilocalories
#'
#' @return grams of fat
#'
#' @examples
#' kcal_to_fat(100)
#' @rdname kcal_to_fat
#' @export
kcal_to_fat <- function(kcal) {
  checkmate::assert_number(kcal, lower = 0)
  fat <- kcal * (1 / 9)
  return(fat)
}

#' @title Kcal to protein
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert kcal to protein in gram
#'
#' @param kcal kilocalories
#'
#' @return grams of protein
#'
#' @examples
#' kcal_to_protein(100)
#' @rdname kcal_to_protein
#' @export
kcal_to_protein <- function(kcal) {
  checkmate::assert_number(kcal, lower = 0)
  protein <- kcal * 0.25
  return(protein)
}


#' @title Kcal to joule
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert kcal to joule
#'
#' @param kcal kilocalories
#'
#' @return Joule in kJ
#'
#' @details One kilocalorie (equivalent to 4.184 kJ) is the amount of heat
#'   required to raise the temperature of 1 kg of water 1°C (e.g., from 15 to
#'   16°C) at standard atmospheric pressure (760 mm Hg).
#'
#' @references
#' National Research Council (US) Committee on Diet and Health. Diet and Health:
#' Implications for Reducing Chronic Disease Risk. Washington (DC): National
#' Academies Press (US); 1989. 6, Calories: Total Macronutrient Intake, Energy
#' Expenditure, and Net Energy Stores. Available from:
#' https://www.ncbi.nlm.nih.gov/books/NBK218769/
#'
#' @examples
#' kcal_to_joule(100)
#' @rdname kcal_to_joule
#' @export
kcal_to_joule <- function(kcal) {
  checkmate::assert_number(kcal, lower = 0)
  joule <- kcal * 4184
  return(joule)
}

#' @title Fat (g) to kcal
#'
#' @description
#' \lifecycle{stable}
#'
#' Convert grams of fat to kcal
#'
#' @param fat fat in grams
#'
#' @return kcal
#'
#' @examples
#' fat_to_kcal(100)
#' @rdname fat_to_kcal
#' @export
fat_to_kcal <- function(fat) {
  checkmate::assert_number(fat, lower = 0)
  kcal <- fat * 7.7
  return(kcal)
  # TODO (MJABOER): Implement article below
  # https://www.nature.com/articles/0803720.pdf?origin=ppub
}
MarijnJABoer/befitteR documentation built on April 24, 2020, 5:43 a.m.