Reputation: 493
I have the following data:
d <- data.frame(
ID= c("NULL", "NULL", "1232", "4565", "4321"))
I'm trying to create a new line that shows "missing" when the ID is NULL, and "not missing" when the ID is not NULL. I have the following code:
d %>%
mutate(ID_missing= case_when(ID=="NULL") ~ "missing",
ID!="NULL" ~ "not missing", TRUE ~ NA_real_) -> d
however I get the following error:
Error in mutate_impl(.data, dots) :
Column `name_of` is of unsupported type quoted call
I can't see any guidance on line and I can't see what might be wrong with my code. Any ideas?
Upvotes: 0
Views: 1419
Reputation: 653
Below would work .
require(dplyr)
d <- data.frame(
ID= c("NULL", "NULL", "1232", "4565", "4321"))
#### USE mutate and ifelse
d <- d %>% mutate(ID_missing = ifelse(ID == "NULL","missing","not_missing"))
Upvotes: 0
Reputation: 70336
There are two problems in your approach:
1) parenthesis
Your use of case_when
is incorrect because of the closing parenthesis in the middle of the function. It should be
case_when(ID=="NULL" ~ "missing",
ID!="NULL" ~ "not missing",
TRUE ~ NA_real_))
2) Incorrect NA-type
You're using NA_real_
inside a character column. You need to use NA_character_
instead.
The final would then be:
d %>%
mutate(ID_missing= case_when(ID=="NULL" ~ "missing",
ID!="NULL" ~ "not missing", TRUE ~ NA_character_)) -> d
# ID ID_missing
# 1 NULL missing
# 2 NULL missing
# 3 1232 not missing
# 4 4565 not missing
# 5 4321 not missing
Upvotes: 4