简体   繁体   中英

Removing characters from string in R

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:

[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:

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

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

Where is my mistake?

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

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

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

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

Update

Suppose, if there are multiple words in the string, the gsub and str_replace_all would provide the same output.

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 strip from qdap

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM