简体   繁体   English

在 R 中搜索和替换字符串、不匹配的模式和 NA

[英]search and replace strings, not matching pattern, and NA in R

I have character vector (column) in a data.frame that contains three correct values comprised of "Positive", "Negative" and NA, along a small number of incorrect values.我在 data.frame 中有字符向量(列),其中包含三个正确的值,包括“Positive”、“Negative”和 NA,以及少量不正确的值。 I want to replace the incorrect values, ie everything except "Positive", "Negative" and "NA" with "Positive" I can grep for NOT "Positive" or "Negative", using grep and the invert=TRUE argument, and get everything except instances of "Negative" and "Positive" but I've not figured out how to additionally, not replace any instances of NA.我想用“Positive”替换不正确的值,即除了“Positive”、“Negative”和“NA”之外的所有内容,我可以使用 grep 和 invert=TRUE 参数对 NOT“Positive”或“Negative”进行 grep,然后得到除了“负”和“正”的实例之外的所有东西,但我还没有想出如何另外,不替换任何 NA 实例。 I've also looked at the sub command, but it does not have an invert option.我还查看了 sub 命令,但它没有反转选项。 Any suggestions?有什么建议?

This should work:这应该有效:

df[!is.na(df$column) & !df$column %in% 
        c("Positive","Negative"), "column"] <- "Positive"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM