I have several dataframes with same column names. I wrote a code to perform some operations on a column based on several conditions. But I got error if all the values of that column is empty.
For an example, if my dataframe is like below, my code works fine.
| unit | temperature |
|---|---|
| C | 70 |
| C | |
| F | 20 |
| K | 130 |
But if my dataframe is like below, my code gives error.
| unit | temperature |
|---|---|
| K | |
| C | |
| F | |
| C |
My code is,
data <- data %>%
mutate(temperature = case_when(
unit == 'C' ~ temperature,
unit == 'F' ~ (temperature - 32)*(5/9),
unit == 'K' ~ temperature - 273))
Could someone help?
>Solution :
You can force your empty column (it does not matter if it contains empty strings or logical NA’s) to a specific data type, i.e. in this case to a numeric class. Then your case_when() should work and should return NA (NA_real_)
library(dplyr)
tibble::tribble(~unit, ~temperature,
"K","",
"C","",
"F","",
"C", "") %>%
mutate(
temperature = temperature %>% as.numeric(),
temperature = case_when(
unit == 'C' ~ temperature,
unit == 'F' ~ (temperature - 32)*(5/9),
unit == 'K' ~ temperature - 273))