use*_*713 10 string whitespace r special-characters
我有这样的字符串的字符向量:
x <- c("weather is good_today","it. will rain tomorrow","do not* get_angry")
Run Code Online (Sandbox Code Playgroud)
我想替换所有特殊字符和空格,并用"_"替换它们.我用str_replace all
从stringr package
这样的:
x1 <- str_replace_all(x,"[[:punct:]]","_")
x2 <- str_replace_all(x1,"\\s+","_")
Run Code Online (Sandbox Code Playgroud)
但这可以在一个命令中完成,我可以得到这样的输出:
x
[1]"weather_is_good_today"
[2]"it_will_rain_tomorrow"
[3]"do_not_get_angry"
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
ags*_*udy 20
gsub('([[:punct:]])|\\s+','_',x)
"weather_is_good_today" "it__will_rain_tomorrow" "do_not__get_angry"
Run Code Online (Sandbox Code Playgroud)
尝试这个 。
x1 <- str_replace_all(x,"[[:punct:]\\\s]+","_")
Run Code Online (Sandbox Code Playgroud)
我没有 R 方面的知识,我根据Wiki检查的正则表达式建议了答案