Remove leading backslash from string R

孤者浪人 提交于 2019-12-02 07:21:21

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.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!