Open In App

Temperature Conversion in R

Last Updated : 27 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Discover the art of precise temperature conversion in R with our comprehensive article. Uncover the secrets behind seamlessly converting Celsius, Fahrenheit, and Kelvin scales using R programming. Whether you’re a data enthusiast or a scientific researcher, this article equips you with the tools and knowledge to master temperature conversions effortlessly. Say goodbye to temperature confusion and hello to accuracy in your R projects.

Concepts

Celsius (°C): The Celsius scale is a commonly used temperature scale in which 0 degrees Celsius represents the freezing point of water and 100 degrees Celsius represents the boiling point of water at standard atmospheric pressure.

Fahrenheit (°F): The Fahrenheit scale is mainly used in the United States. In this scale, 32 degrees Fahrenheit represents the freezing point of water, and 212 degrees Fahrenheit represents the boiling point of water at standard atmospheric pressure.

Kelvin (K): The Kelvin scale is a scientific temperature scale that starts from absolute zero, the lowest possible temperature at which all molecular motion ceases. Absolute zero is defined as 0 Kelvin.

Conversion Formulas:

To convert from Celsius to Kelvin:

Kelvin = Celsius + 273.15

To convert from Celsius to Fahrenheit:

Fahrenheit = (Celsius * 9/5) + 32

To convert from Fahrenheit to Celsius:

Celsius = (Fahrenheit – 32) * 5/9

To convert from Kelvin to Celsius:

Celsius = Kelvin – 273.15

To convert from Fahrenheit to Kelvin:

Kelvin = (Fahrenheit – 32) * 5/9 + 273.15

Converting Celsius to Kelvin

R




# Convert Celsius to Kelvin
celsius_to_kelvin <- function(celsius) {
  return(celsius + 273.15)
}
 
# Example: Convert 25 degrees Celsius to Kelvin
celsius_temp <- 25
kelvin_temp <- celsius_to_kelvin(celsius_temp)
cat(celsius_temp, "degrees Celsius is equal to", kelvin_temp, "Kelvin\n")


Output:

25 degrees Celsius is equal to 298.15 Kelvin

  • We start with 25 degrees Celsius.
  • The celsius_to_kelvin function is used to convert this temperature to Kelvin using the formula: Kelvin = Celsius + 273.15.
  • The result is printed, stating that 25 degrees Celsius is equal to the calculated value in Kelvin.

Celsius to Fahrenheit Conversion

R




# Convert Celsius to Fahrenheit
celsius_to_fahrenheit <- function(celsius) {
  return((celsius * 9/5) + 32)
}
 
# Example: Convert 25 degrees Celsius to Fahrenheit
celsius_temp <- 25
fahrenheit_temp <- celsius_to_fahrenheit(celsius_temp)
cat(celsius_temp, "degrees Celsius is equal to", fahrenheit_temp, "Fahrenheit\n")


Output:

25 degrees Celsius is equal to 77 Fahrenheit

  • We start with 25 degrees Celsius.
  • The celsius_to_fahrenheit function is used to convert this temperature to Fahrenheit using the formula: Fahrenheit = (Celsius * 9/5) + 32.
  • The result is printed, stating that 25 degrees Celsius is equal to the calculated value in Fahrenheit.

Fahrenheit to Celsius Conversion

R




# Convert Fahrenheit to Celsius
fahrenheit_to_celsius <- function(fahrenheit) {
  return((fahrenheit - 32) * 5/9)
}
 
# Example: Convert 77 degrees Fahrenheit to Celsius
fahrenheit_temp <- 77
celsius_temp <- fahrenheit_to_celsius(fahrenheit_temp)
cat(fahrenheit_temp, "degrees Fahrenheit is equal to", celsius_temp, "Celsius\n")


Output:

77 degrees Fahrenheit is equal to 25 Celsius

  • We start with 77 degrees Fahrenheit.
  • The fahrenheit_to_celsius function is used to convert this temperature to Celsius using the formula: Celsius = (Fahrenheit – 32) * 5/9.
  • The result is printed, stating that 77 degrees Fahrenheit is equal to the calculated value in Celsius.

Kelvin to Celsius Conversion

R




# Convert Kelvin to Celsius
kelvin_to_celsius <- function(kelvin) {
  return(kelvin - 273.15)
}
 
# Example: Convert 298.15 Kelvin to Celsius
kelvin_temp <- 298.15
celsius_temp <- kelvin_to_celsius(kelvin_temp)
cat(kelvin_temp, "Kelvin is equal to", celsius_temp, "degrees Celsius\n")


Output:

298.15 Kelvin is equal to 25 degrees Celsius

  • We start with 298.15 Kelvin.
  • The kelvin_to_celsius function is used to convert this temperature to Celsius using the formula: Celsius = Kelvin – 273.15.
  • The result is printed, stating that 298.15 Kelvin is equal to the calculated value in Celsius.

Fahrenheit to Kelvin Conversion

R




# Convert Fahrenheit to Kelvin
fahrenheit_to_kelvin <- function(fahrenheit) {
  return((fahrenheit - 32) * 5/9 + 273.15)
}
 
# Example: Convert 77 degrees Fahrenheit to Kelvin
fahrenheit_temp <- 77
kelvin_temp <- fahrenheit_to_kelvin(fahrenheit_temp)
cat(fahrenheit_temp, "degrees Fahrenheit is equal to", kelvin_temp, "Kelvin\n")


Output:

77 degrees Fahrenheit is equal to 298.15 Kelvin

  • We start with 77 degrees Fahrenheit.
  • The fahrenheit_to_kelvin function is used to convert this temperature to Kelvin using the formula: Kelvin = (Fahrenheit – 32) * 5/9 + 273.15.
  • The result is printed, stating that 77 degrees Fahrenheit is equal to the calculated value in Kelvin.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads