使用 R 到 Gsub 搜索 *

Using R to Gsub search for *

其他人肯定遇到过这个问题,但我找不到其他用户发布这个问题,所以我会的。

v <- gsub( "*" , "" , "All Large Firms*" )

我希望 v 成为 "All Large Firms" 但它无法将“*”识别为文本

我们需要对 * 进行转义,因为它是一个特殊字符,表示 0 个或多个字符。当我们转义(\)时,它将被视为任何其他字符。

gsub( "\*" , "" , "All Large Firms*" )

或者我们可以把它放在方括号内

gsub( "[*]" , "" , "All Large Firms*" )

或者正如@Richard Scriven 所建议的那样,如果您只想删除 * 并且不使用任何其他正则表达式模式,也可以使用 fixed=TRUE 参数(这会很快)

gsub( "*" , "" , "All Large Firms*", fixed=TRUE)