简体   繁体   中英

Replace single backslash in R

I have a string that looks like:

str<-"a\f\r"

One quite universal solution is

gsub("\\\\", "", str)

Thanks to the comment above.

When inputting backslashes from the keyboard, always escape them.

str <-"this\\is\\my\\string"    # note doubled backslashes -> 'this\is\my\string'
gsub("\\", "", str, fixed=TRUE) # ditto

str2 <- "a\\f\\r"               # ditto -> 'a\f\r'
gsub("\\", "", str2, fixed=TRUE)# ditto

Note that if you do

str <- "a\f\r"

then str contains no backslashes. It consists of the 3 characters a , \\f (which is not normally printable, except as \\f , and \\r (same).

And just to head off a possible question. If your data was read from a file, the file doesn't have to have doubled backslashes. For example, if you have a file test.txt containing

a\b\c\d\e\f

and you do

str <- readLines("test.txt")

then str will contain the string a\\b\\c\\d\\e\\f as you'd expect: 6 letters separated by 5 single backslashes. But you still have to type doubled backslashes if you want to work with it.

str <- gsub("\\", "", str, fixed=TRUE)  # now contains abcdef

From the dput , it looks like what you've got there is UTF-16 encoded text, which probably came from a Windows machine. According to

it encodes glyphs in the Supplementary Multilingual Plane , which is pretty obscure. I'll guess that you need to supply the argument encoding="UTF-16" to readLines when you read in the file.

由于没有任何直接的方法来处理单个反斜杠,这是 David Arenburg 在评论部分提供的最接近问题的解决方案

gsub("[^A-Za-z0-9]", "", str) #remove all besides the alphabets & numbers

This might be helpful :)

require(stringi)
stri_escape_unicode("ala\\ma\\kota")
## [1] "ala\\\\ma\\\\kota"
stri_unescape_unicode("ala\\ ma\\ kota")
## [1] "ala ma kota"

This is the same as the accepted answer but rtemoves less (just non-ascii characters):

gsub("[^ -~]", '', "a\f\r") 
## [1] "a"

As of R 4.0.0, you can now use raw strings to avoid confusion with backlashes, just use the following syntax: r"(your_raw_expression)"<\/code> (parentheses included):

str<-r"(ud83d\ude21\ud83d\udd2b)" #Equivalent of "ud83d\\ude21\\ud83d\\udd2b"
gsub(r"(\\)", "", str)
# [1] "ud83dude21ud83dudd2b"

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