library(tidyverse)
df <- tibble(ID = c("ABC", "EFG", "HIJ", "KLM", "NOP", "QRS"),
Date = as.Date(c("2019-01-03", "2019-01-08",
"2019-06-09", "2019-06-11",
"2019-08-12", "2019-08-21")))
#> # A tibble: 6 x 2
#> ID Date
#> <chr> <date>
#> 1 ABC 2019-01-03
#> 2 EFG 2019-01-08
#> 3 HIJ 2019-06-09
#> 4 KLM 2019-06-11
#> 5 NOP 2019-08-12
#> 6 QRS 2019-08-21
Let's start with the data frame above. What I want is shown directly below. The first two line items in the data frame satisfy the conditions in my case_when()
statement, and are populated with "fizz" and "buzz". The remainder is populated with NA
.
df %>%
mutate(col3 = case_when(ID == "ABC" & Date == as.Date("2019-01-03") ~ "fizz",
ID == "EFG" & Date == as.Date("2019-01-08") ~ "buzz"))
#> # A tibble: 6 x 3
#> ID Date col3
#> <chr> <date> <chr>
#> 1 ABC 2019-01-03 fizz
#> 2 EFG 2019-01-08 buzz
#> 3 HIJ 2019-06-09 NA
#> 4 KLM 2019-06-11 NA
#> 5 NOP 2019-08-12 NA
#> 6 QRS 2019-08-21 NA
Yet when I try to explicitly tell the case_when()
function to populate the remainder of the data frame with NA
I get the error shown below? Am I not utilizing TRUE ~ NA
in the correct fashion?
Doesn't the TRUE ~ _XYZ_
argument tell the function to populate any condition not satisfied by conditions above with _XYZ_
?
df %>%
mutate(col3 = case_when(ID == "ABC" & Date == as.Date("2019-01-03") ~ "fizz",
ID == "EFG" & Date == as.Date("2019-01-08") ~ "buzz",
TRUE ~ NA)
#> Error: unexpected ',' in "
#> ID == "EFG" & Date == as.Date("2019-01-08") ~ "buzz","
Aucun commentaire:
Enregistrer un commentaire