string 从R中的字符串中删除字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27044727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:19:06  来源:igfitidea点击:

Removing characters from string in R

rstring

提问by ben_aaron

I have some tabs or space breaks in my R output (I suspect the problem in the task from which the output comes), making it look like this:

我的 R 输出中有一些制表符或空格符(我怀疑输出来自任务中的问题),使其看起来像这样:

[1841] "\t\t\tGreen\n\t\t"         
[1842] "Blue"                       
[1843] "\t\t\tRed\n\t\t" 

For a colleague I have to read this into SPSS and that gives some trouble when reading this as txt data, so I wanted to remove the \t and \n parts in my string:

对于一位同事,我必须将其读入 SPSS,这在将其作为 txt 数据读取时会出现一些问题,因此我想删除字符串中的 \t 和 \n 部分:

str_replace(mydata, "([\n])", "")

Tried it with both \n and \t or combinations, but never quite worked.

尝试了 \n 和 \t 或组合,但从未奏效。

Where is my mistake?

我的错误在哪里?

回答by hwnd

You need to use str_replace_allto remove the multiple accounts of whitespace characters. Why not use base R to remove these characters instead of loading the stringrpackage?

您需要使用str_replace_all删除多个空格字符的帐户。为什么不使用 base R 来删除这些字符而不是加载stringr包?

gsub('[\t\n]', '', mydata)

回答by akrun

Try

尝试

library(stringr)
str1 <- c("\t\t\tGreen\n\t\t", "Blue",  "\t\t\tRed\n\t\t" )
str_replace_all(str1, "([\n\t])", "")

#[1] "Green" "Blue"  "Red"  

Or using stringi

或使用 stringi

library(stringi)
stri_replace_all_regex(str1, "[\n\t]", "")
#[1] "Green" "Blue"  "Red"  

Update

更新

Suppose, if there are multiple words in the string, the gsuband str_replace_allwould provide the same output.

假设,如果字符串中有多个单词,则gsubstr_replace_all将提供相同的输出。

x <- c("\t\t\tGreen\n\t\t", "Blue", "\t\t\tRed\n\t\t yellow")
str_replace_all(x, '[\n\t]', '')
#[1] "Green"      "Blue"       "Red yellow"

Another option would be to use stripfrom qdap

另一种选择是使用stripfromqdap

library(qdap)
strip(x, lower.case=FALSE)
#[1] "Green"      "Blue"       "Red yellow"
## Or...
Trim(clean(x))
#[1] "Green"      "Blue"       "Red yellow"