簡體   English   中英

R 從字符串中刪除(NA)

[英]R remove (NA) from a string

我有

"this is an example (NA)"

並希望有"this is an example"

gsub("(NA)", "", "this is an example (NA)")

不起作用。

我試過gsub

gsub("(NA)", "", "this is an example (NA)")
[1] "this is an example ()"

你可以試試:

"this is an example (NA)" -> x
gsub('\\(NA)', '', x)

另一種可能的解決方案,基於stringr

library(stringr)

string <- "this is an example (NA)"
str_remove_all(string, "\\s*\\(NA\\)\\s*")

#> [1] "this is an example"

您應該啟用fixed = TRUE

> gsub("(NA)", "", "this is an example (NA)", fixed = TRUE)
[1] "this is an example "
string <- "this is an example (NA)"
gsub(' \\(NA\\)', '', string)
[1] "this is an example"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM