2012-06-20 12 views
5

Con R, quando abbiamo messo i doppi apici all'interno di una virgolette doppie:risolvere il qoutes raddoppierà entro numero doppio citazioni in R

y <- " " " " 

andrà a finire fuori

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

La mia domanda è come rimuovere tutte le doppie virgolette rilevate o convertite in virgolette singole tra virgolette doppie.

Ad esempio:

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

Il risultato desiderato è

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

o

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

risposta

5

Stai analizzando l'input di stringa da un file o da uno standard input di qualcosa?

scan(what='character',sep='\n') legge i dati dallo stdin() e sfugge automaticamente alle virgolette. Lo stesso se da un file

>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\"\"" 

Una volta che hai il tuo ingresso che si potrebbe usare un'espressione regolare per sostituire le citazioni interne escape ... (credo! - potrebbe essere un complicato reg exp)

+0

Grazie mille. Questo è quello che sto cercando! > y <- scan (what = "character", sep = "\ n", allowEscapes = T) Sto guardando "Prometeo". y –

4

Im probabilmente non ottenerlo, ma

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

o

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." 
Problemi correlati