tags:

views:

106

answers:

2

Hi All, I am trying to print out a line that contains a mixture of a String and a variable. Here is the R code at present:

cat("<set name=\",df$timeStamp,\" value=\",df$Price,\" ></set>\n")

Here is what it prints out when run:

<set name=",df$timeStamp," value=",df$Price," ></set>

I would like it to have the value of df$timeStamp and df$Price printed out. I would like for example the following:

<set name="2010-08-18 12:00:59" value="17.56" ></set>

Any idea where I am going wrong in the code?

Any and all help greatly appreciated.

Regards,

Anthony.

+3  A: 

You're missing some extra quotes. Try this:

cat('<set name=\"',df$timeStamp,'\" value=\"',df$Price,'\" ></set>\n')

Here's a working example of what I mean:

cat("a b c -> \"", letters[1:3], "\"\n")
Shane
If you want to be super-slick, you can avoid having to escape quotes (in this case) by using single-quotes to delineate the string and double-quotes within it: `cat('a b c -> "', letters[1:3], '"\n')`
Joshua Ulrich
+4  A: 

The problem is that R does not evaluate any expression withing the quotes - it is just printing the string you defined. There are multiple ways around it. For example, you can use the sprintf function (untested because you do not provide a reproducible example):

   cat(sprintf("<set name=\"%s\" value=\"%f\" ></set>\n", df$timeStamp, df$Price))
Aniko