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"
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With