从字符串R中删除前导反斜杠

编程入门 行业动态 更新时间:2024-10-14 04:23:22
本文介绍了从字符串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中删除前导反斜杠

本文发布于:2023-11-06 08:15:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   前导   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!