Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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
如何在特定文本匹配中替换R中的值NA_R_Dplyr - Fatal编程技术网

如何在特定文本匹配中替换R中的值NA

如何在特定文本匹配中替换R中的值NA,r,dplyr,R,Dplyr,我们可以使用grepl。它检查备注中的错误字符串,并替换为NA library(dplyr) sample_df %>% mutate(Remark= case_when(grepl("Error", Remark) ~ "NA", TRUE ~ Remark)) 输出: # A tibble: 5 x 2 `Product Code` Remark <d

我们可以使用
grepl
。它检查
备注
中的错误字符串,并替换为NA

library(dplyr)

sample_df %>% 
  mutate(Remark= case_when(grepl("Error", Remark) ~ "NA",
                           TRUE ~ Remark))
输出:

# A tibble: 5 x 2
  `Product Code` Remark
           <dbl> <chr> 
1       12200234 Done  
2       12200234 Done  
3       12200205 NA    
4       12200256 NA    
5       12200126 NA  
#一个tible:5 x 2
`产品代码`备注
112200234完成
212200234完成
312200205北美
412200256 NA
5 12200126 NA
需要(gData)
myDf$Remark[startsWith(myDf$Remark,“Error”)]另一个选项

library(stringr)
library(dplyr)
sample_df %>% 
    mutate(Remark= case_when(str_detect(Remark, "Error") ~ NA_character_,
                       TRUE ~ Remark))
# A tibble: 5 x 2
  `Product Code` Remark
           <dbl> <chr> 
1       12200234 Done  
2       12200234 Done  
3       12200205 NA    
4       12200256 NA    
5       12200126 NA  
require(gData)

myDf$Remark[startsWith(myDf$Remark, "Error")] <- NA

library(stringr)
library(dplyr)
sample_df %>% 
    mutate(Remark= case_when(str_detect(Remark, "Error") ~ NA_character_,
                       TRUE ~ Remark))