Search code examples
rmutate

Convert temperature data from Kelvin to Celsius in multiple columns


I have a dataset with multiple columns of temperature data in Kelvin. I want to convert it to celsius.

Sr. StationID   MaxTemp   MinTemp  MeanTemp  Prec   
1   5320         280       270       275      1.2

I am using this code which helps to convert only one column (MxTemp). I want to add more column names.

DF %>%
  mutate(across(.cols = starts_with("MaxTemp"),.fns = function(x) x - 273.15))

Solution

  • instead of starts_with, you could use ends_with:

    DF %>% mutate(across(.cols = ends_with("Temp"),.fns = function(x) x - 273.15))