简体   繁体   中英

Remove leading backslash from string R

Here is the string:

> raw.data[27834,1]
[1] "\xff$GPGGA"

I have tried advice from the following two questions, but with no luck:

How to escape a backslash in R?

How to escape backslashes in R string

Does anyone have a different solution from the above questions that might help? The ideal solution would be to remove the "\\xff" portion, but for any combination of letters.

There is no backslash in that string. The displayed backslash is an escape marker. This and other features about entry and display of "special situations" are described in the ?Quotes help page.. You've been given one regex rather elliptical approach to removal. Here are a couple of other approaches .... only some of which actually succeed because the \\ff is the first "character" and it's not really legal as an R character:

 s <- "\xff$GPGGA"
 strsplit(s, "")
#[[1]]
#[1] NA

Warning message:
In strsplit(s, "") : input string 1 is invalid in this locale

 substr(s, 1,1)
#Error in substr(s, 1, 1) : invalid multibyte string at '<ff>$GP<47>GA'
 gsub('.*([^A-Za-z].*)', '\\1',"\xff$GPGGA")#[1]
#[1] "$GPGGA"
 ?Quotes
 gsub('\xff', '',"\xff$GPGGA")#[1]
#[1] "$GPGGA"

I think the reason that the regex functions don't choke on that string is that regex is actually a system mediated process whereas strsplit and substr are internal R functions.

@RichardScriven posts an example and when I tried to replicated it, I get yet a different example that shows the mapping to displayed characters is system specific. I'm on OSX 10.10.1 (Yosemite)>

cat('\xff')
ˇ

(I left off the octothorpe (#) that I would normally out in.)

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