[R] write.csv to text string?

Sarah Goslee sarah.goslee at gmail.com
Thu Mar 12 19:10:15 CET 2015


It's really not a job for the write.* functions, but for the
string-handling functions.

Here's a slightly clunky possibility:

# use your example
s.df <- read.csv(text='cola,colb\n1,2\n2,3\n')

# turn a data frame into a string
paste(
   paste(colnames(s.df), collapse=","),
   paste(apply(s.df, 1, paste, collapse=","), collapse="\n"),
   sep="\n")


On Thu, Mar 12, 2015 at 1:15 PM, Thomas Nyberg <tomuxiong at gmail.com> wrote:
> Hello,
>
> I've found the following useful functionality:
>
>> s <- 'cola,colb\n1,2\n2,3\n'
>> read.csv(text=s)
>   cola colb
> 1    1    2
> 2    2    3
>
>
> But I haven't found a similar option in write.csv. I.e. I would like to
> "write" a dataframe to a string. What would be the easiest way to go
> about such a thing? Right now I can only think of using a file as an
> intermediary, but that seems a bit silly. Thanks for any help.
>
> Cheers,
> Thomas Nyberg


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list