-1

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))
Edward
  • 10,360
  • 2
  • 11
  • 26
Sana
  • 65
  • 6

2 Answers2

0

instead of starts_with, you could use ends_with:

DF %>% mutate(across(.cols = ends_with("Temp"),.fns = function(x) x - 273.15))
mweylandt
  • 108
  • 5
0

solution using data.table

library(data.table)
setDT(DF) # make it a data.table

tcols = grep("Temp", names(DF))
DF[, (tcols) := .SD - 273.15, .SDcols = tcols] # columns updated by reference
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22