【问题标题】:Solve the Double qoutes within double quotes issue in R解决 R 中双引号内的双引号问题
【发布时间】:2012-06-20 10:11:44
【问题描述】:

使用 R,当我们将双引号放在双引号内时:

y <- " " " "

会结束的

错误:"y

我的问题是如何删除所有检测到的双引号或在主双引号内转换成单引号。

例如:

y <- "I'm watching "Prometheus"." 
y

想要的结果是

#[1] "I'm watching Prometheus."

#[1] "I'm watching 'Prometheus'."

【问题讨论】:

    标签: r


    【解决方案1】:

    您是在解析来自文件的字符串输入还是某些东西的标准输入?

    scan(what='character',sep='\n') 将从stdin() 读取数据并自动转义引号。如果来自文件,则相同

    >scan(what="character",sep="\n",allowEscapes=T)
    1: I'm watching "Prometheus"
    2: 
    Read 1 item
    [1] "I'm watching \"Prometheus\""
    >
    >scan(what="character",sep="\n",allowEscapes=T)
    1: "I'm watching "Prometheus""
    2: 
    Read 1 item
    [1] "\"I'm watching \"Prometheus\"\""
    

    一旦您得到输入,您就可以使用正则表达式来替换转义的内引号...(我认为!- 可能是一个复杂的 reg exp)

    【讨论】:

    • 非常感谢。这就是我要找的! > y
    【解决方案2】:

    我可能没听懂,但是

    gsub("\"","","I'm watching \"Prometheus\".") 
    

    gsub("\"","'","I'm watching \"Prometheus\".") 
    

    ?

    【讨论】:

      【解决方案3】:
      y <-  "I\'m watching 'Prometheus'."
      
      [1] "I'm watching 'Prometheus'."
      
      y <-  "I\'m watching Prometheus."
      
      [1] "I'm watching Prometheus."
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-17
        • 1970-01-01
        • 1970-01-01
        • 2017-11-18
        • 1970-01-01
        • 2014-04-07
        相关资源
        最近更新 更多