master
dplyr
Re-write our function to work with missing values.
Note you need to make the NA check first.
NA
k_c <- function(temp_k) { if (is.na(temp_k)) { return(NA) } else if (temp_k < 0) { warning('you passed in a negative Kelvin number') # stop() return(NA) } else { temp_c <- temp_k - 273.15 return(temp_c) } }
k_c(-9) ## Warning in k_c(-9): you passed in a negative Kelvin number ## [1] NA
k_c(NA) ## [1] NA
k_c(0) ## [1] -273.15