Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error while case_when() applied on empty column in R

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading