Monday, September 14, 2009

Converting Vectors to Strings

Just a quick one, as I just created the blog, and I have a meeting in ten minutes.

I find it surprisingly difficult to convert a vector to a comma-separated string. Since I work with Sweave and LaTeX a lot this comes up, if I want to output the names of a dataset into a paragraph. I thought that paste or cat would do it, but paste doesn't work and cat doesn't output to the LaTeX file.

The solution is the function toString, and it works just like paste:

> toString(1:10,sep=', ')
[1] "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"


Happy coding!

No comments:

Post a Comment