[R] pasting a formula string with double quotes in it

Rui Barradas ruipbarradas at sapo.pt
Fri Apr 20 20:15:06 CEST 2012


Hello,


StellathePug wrote
> 
> Hello everyone,
> I have tried several ways of doing this and searched the documentation and
> help lists and I have been unable to find an answer or even whether it is
> possible to do it. I am pasting together a formula and I need to insert
> double quotes around the strings. Here's an example:
> 
>   location <- c("AL", "AK", "MA", "PA")
>   v=2
>   test <-  cbind(
>                " <- cbind(df$DATE[df$LOCATION %in% c(",
>                location[v], 
>                ")], df$WTD.AVG.PRICE[df$LOCATION %in% c(",
>                location[v],           
>                ")])")
>   
>   test <- paste(test, collapse="")
>   test
>  
> Solution:
> [1] " <- cbind(df$DATE[df$LOCATION %in% c(AK)],
> df$WTD.AVG.PRICE[df$LOCATION %in% c(AK)])"
> 
> This does not produce an error message but it is obviously wrong because I
> need double quotes around AK, i.e.,
> " <- cbind(df$DATE[df$LOCATION %in% c("AK")], df$WTD.AVG.PRICE[df$LOCATION
> %in% c("AK")])"
> And obviously, I gave R no indication whatsoever to use double quotes.
> 
> 
> If I use 
>  test <- paste(test, collapse=""") 
> it does not work, which makes sense because the second double quote closes
> the first and the third is left an orphan.
> 
> Anyone have any ideas?
> 
> Thanks and have a great weekend!
> Rita
> 

Use single quotes instead, they don't close the double quotes and, for this,
the result is the same.
Look at the end/beginning of the lines before/after location[v]

test2 <-  cbind(
               " <- cbind(df$DATE[df$LOCATION %in% c('",
               location[v],
               "')], df$WTD.AVG.PRICE[df$LOCATION %in% c('",
               location[v],          
               "')])")
test2 <- paste(test2, collapse="")
test2

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/pasting-a-formula-string-with-double-quotes-in-it-tp4574419p4574888.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list