Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing characters from string in R

Tags:

string

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?

like image 417
ben_aaron Avatar asked Nov 20 '14 16:11

ben_aaron


2 Answers

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)
like image 96
hwnd Avatar answered Nov 09 '22 09:11

hwnd


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"
like image 30
akrun Avatar answered Nov 09 '22 09:11

akrun