从字符串R中删除前导反斜杠 [英] Remove leading backslash from string R

查看:205
本文介绍了从字符串R中删除前导反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是字符串:

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

我已经尝试过以下两个问题的建议,但没有运气:





如何在R字符串中转义反斜杠



有没有人有不同的解决方案以上可能有帮助的问题?理想的解决方案是删除\xff部分,但可以使用任何字母组合。

解决方案

该字符串中没有反斜杠。显示的反斜杠是一个转义标记。有关特殊情况的输入和显示的这个和其他功能在?行情帮助页面中有描述。您已经获得了一个正则表达式相当椭圆的删除方法。这里有几个其他方法....只有其中一些实际上成功,因为 \ff 是第一个字符,它不是真正合法的R字符

$ b

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

警告信息:
在strsplit(s,)中:输入字符串1在此语言环境中无效

substr(s,1,1)
在substr(s,1,1)中的#Error:'< ff> $ GP< 47>无效多字节字符串GA'
gsub('。*([^ A-Za-z]。*)','\\1',\xff $ GPGGA)#[1]
#[1]$ GPGGA
?行情
gsub('\xff','','\xff $ GPGGA)#[1]
#[1]$ GPGGA
我认为正则表达式函数不会阻塞该字符串的原因是正则表达式实际上是一个系统介入的过程,而/ strsplit substr 是内部R函数。



RichardScriven发表了一个例子,当我试图重新给出了一个不同的示例,显示了显示字符的映射是系统特定的。我在OSX 10.10.1(优胜美地)>

  cat('\xff')

(我离开了我通常会出现的octothorpe(#)。)


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

这篇关于从字符串R中删除前导反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆