Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr变异问题案例_R_Dplyr - Fatal编程技术网

dplyr变异问题案例

dplyr变异问题案例,r,dplyr,R,Dplyr,我有以下数据: d <- data.frame( ID= c("NULL", "NULL", "1232", "4565", "4321")) 但是,我得到以下错误: Error in mutate_impl(.data, dots) : Column `name_of` is of unsupported type quoted call 我在网上看不到任何指导,也看不到我的代码可能有什么问题。有什么想法吗?你的方法有两个问题: 1) 括号 你使用 CaseEy时,

我有以下数据:

d <- data.frame(
        ID= c("NULL", "NULL", "1232", "4565", "4321"))
但是,我得到以下错误:

Error in mutate_impl(.data, dots) : 
Column `name_of` is of unsupported type quoted call

我在网上看不到任何指导,也看不到我的代码可能有什么问题。有什么想法吗?

你的方法有两个问题:

1) 括号

<>你使用<代码> CaseEy时,由于函数中间的结束括号是不正确的。应该是

case_when(ID=="NULL" ~ "missing", 
          ID!="NULL" ~ "not missing", 
          TRUE       ~ NA_real_))
2) 不正确的NA类型

您正在字符列中使用
NA\u real\u
。您需要改用
NA\u字符

最终结果将是:

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

您的方法存在两个问题:

1) 括号

<>你使用<代码> CaseEy时,由于函数中间的结束括号是不正确的。应该是

case_when(ID=="NULL" ~ "missing", 
          ID!="NULL" ~ "not missing", 
          TRUE       ~ NA_real_))
2) 不正确的NA类型

您正在字符列中使用
NA\u real\u
。您需要改用
NA\u字符

最终结果将是:

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
下面的方法行得通

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"))
require(dplyr)
下面的d将起作用

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"))
require(dplyr)
D