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
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
On Mar 12, 2015, at 10:15 AM, Thomas Nyberg 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.A data.frame is a list structure.> 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.I think you are misunderstanding the structure of a computer file. `write.csv` is already doing essentially what you request. "Files" are essentially "strings" of varying sizes. The operating system print methods are displaying the linefeed/carriage in a manner that breaks the string into useful or readable segments on a "page". If you want to have a text version of what the print.data.frame function returns, then wrap capture.output around print(dfrm). That will produce a character object. --\n David Winsemius Alameda, CA, USA
Hello,
Maybe using text connections. See ?textConnection.
tc <- textConnection("foo", "w")
s <- 'cola,colb\n1,2\n2,3\n'
cat(s, file = tc)
close(tc)
foo
read.csv(text = foo)
Hope this helps,
Rui Barradas
Em 12-03-2015 17:15, Thomas Nyberg escreveu:> 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
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
Or, more in line with what was asked:
con <- textConnection("foo", "w")
write.csv(file=con, airquality)
close(con)
foo
It does, incidentally, look possible to equip write.table (of which write.csv is
a special case) with an intern=TRUE setting, which could effectively do the
above internally and return the result. There is no documented return value from
write.table as far as I can tell.
-pd
> On 12 Mar 2015, at 19:25 , Rui Barradas <ruipbarradas at sapo.pt>
wrote:
>
> Hello,
>
> Maybe using text connections. See ?textConnection.
>
> tc <- textConnection("foo", "w")
> s <- 'cola,colb\n1,2\n2,3\n'
> cat(s, file = tc)
> close(tc)
> foo
>
> read.csv(text = foo)
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 12-03-2015 17:15, Thomas Nyberg escreveu:
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com