Remove leading backslash from string R

假如想象 提交于 2019-12-02 07:36:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/27659160/remove-leading-backslash-from-string-r

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