2012-06-20 17 views
5

で二重引用符の問題内のダブルクォートを解く:は、我々は二重引用符で囲む二重引用符を置くRと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 = "文字"、sep = "\ n"、allowEscapes = T) 「プロメテウス」を見ています。 –

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." 
関連する問題