2012-06-20 32 views
5

隨着R,當我們把一個雙引號雙引號:解決雙引號問題中的雙qoutes中的R

y <- " " " " 

它將結束了

Error: unexpected string constant in "y <- " " " ""

我的問題是如何刪除所有雙引號檢測到或在主雙引號內轉換爲單引號。

例如:

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

期望的結果是

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

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

回答

5

你解析從文件或標準輸入的東西的字符串輸入?

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)

+0

非常感謝。這是我正在尋找的! > y < - 掃描(what =「character」,sep =「\ n」,allowEscapes = T) 我在看「Prometheus」。 y –

4

林可能不會得到它,但

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

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

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

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

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

[1] "I'm watching Prometheus."