[英]dplyr mutate issue case when
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. 我试图创建一个新行,当ID为NULL时显示“丢失”,而当ID不为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?
有任何想法吗?
There are two problems in your approach: 您的方法存在两个问题:
1) parenthesis 1)括号
Your use of case_when
is incorrect because of the closing parenthesis in the middle of the function. 您使用的
case_when
不正确,因为该函数中间的case_when
括号是。 It should be 它应该是
case_when(ID=="NULL" ~ "missing",
ID!="NULL" ~ "not missing",
TRUE ~ NA_real_))
2) Incorrect NA-type 2)不正确的NA类型
You're using NA_real_
inside a character column. 您正在字符列内使用
NA_real_
。 You need to use NA_character_
instead. 您需要改用
NA_character_
。
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
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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.