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?
CodePudding user response:
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))
CodePudding user response:
library(tidyverse)
data <- tribble(
~unit, ~temperature,
"K", NA,
"C", NA,
"F", NA,
"C", NA
)
data %>%
mutate(
temperature = as.numeric(temperature),
temperature = case_when(
unit == "C" ~ temperature,
unit == "F" ~ (temperature - 32) * (5 / 9),
unit == "K" ~ temperature - 273
)
)
#> # A tibble: 4 × 2
#> unit temperature
#> <chr> <dbl>
#> 1 K NA
#> 2 C NA
#> 3 F NA
#> 4 C NA
Created on 2022-06-08 by the reprex package (v2.0.0)