簡體   English   中英

替換R中的文本時出錯

[英]Error in replacing text in R

我用sub替換R中的一個文本。

 x<-"My name is ${name}"
 sub("${name}","Tiger",x)

錯誤信息:

Error in sub("${name}", "Tiger", x) : 
  invalid regular expression '${name}', reason 'Invalid contents of {}'

輸入文本有{},如何修復此錯誤?

${}需要轉義:

sub("\\$\\{name\\}","Tiger",x)
# [1] "My name is Tiger"

使用fixed=TRUE參數:

sub("${name}","Tiger",x, fixed=TRUE)
# [1] "My name is Tiger"

暫無
暫無

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

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