数字后面的连字符分析(text = str)中出现意外的符号错误 [英] Unexpected symbol error in parse(text = str) with hyphen after a digit

查看:106
本文介绍了数字后面的连字符分析(text = str)中出现意外的符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析R中的字符串. 当字符串中有数字后跟连字符时,R抛出意外的符号"或输入的意外结束"异常(请参见代码).搜索并尝试其他方法来解决此问题无济于事. 在我这方面可能有些知识不足. 任何帮助或建议,我们将不胜感激.

I am trying to parse a character string in R. R throws an "unexpected symbol" or "unexpected end of input" exception when there is a digit followed by a hyphen in the string (please see the code). Searching and trying different ways to solve this issue didn't help. Probably some lack of knowledge in my part. Any help or advise would be highly appreciated.

> str <- "abc12-3def"
> parse(text = str)
Error in parse(text = str) : <text>:1:8: unexpected symbol
1: abc12-3def
          ^

> str <- "abc123-"
> parse(text = str)
Error in parse(text = str) : <text>:2:0: unexpected end of input
1: abc123-
  ^

但是,以下示例均能正常工作

However, following examples all work normally

> str <- "abc123def"
> parse(text = str)
expression(abc123def)

> str <- "abc123-def"
> parse(text = str)
expression(abc123-def)

> str <- "abc12-3"
> parse(text = str)
expression(abc12-3)

非常感谢您!

推荐答案

您可以使用以下命令轻松重现parse行为:

You can easily reproduce the parse behavior with :

str <- "3a"
parse(text = str)

parse尝试将str解析为变量名.或者,您应该提供一个可用的变量名,或者它不应该以数字开头或者应该将其放在``之间.以下作品:

parse try to parse your str as a variable name. Or, you should give an available variable name, either it should not begin with a digit or you should put it between ``. the following works :

str <- "`3a`"
parse(text = str)

在您的示例中,这也可以起作用:

and in your example , this works also :

str <- "abc12-`3def`"
parse(text = str)

最后,对于第二个示例,由于您没有提供可用的表达式来解析,因此逻辑上将不起作用:

Finally for your second example , it is logic that it will not work since you don't give an available expression to parse:

str <- "abc123-"  ## this will like myvar-

如果您的-只是一个字符串分隔符,为什么不将其转换为_?例如:

if your - is just a string separator, why not to transform it to _? for example:

 parse(text=gsub('-','_',str))

这篇关于数字后面的连字符分析(text = str)中出现意外的符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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