Dear all,
apologies for this (perhaps recurrent) question but I did not found a question
when searching mailing lists.
How to write a list of a simple kind, e.g.:
abc <- list(one=(1:2), two=(1:5))
# to a file? I understand that write() & co. cannot work but when I try
sink("aa.txt", append=T, split=T)
abc
sink()
# the output is indeed "split by rows" in the textfile, each starting
with row numbers (and I have rather long "vectors" resulting in many
rows...):
$one
[1] 1 2
$two
[1] 1 2 3 4 5
What I would need is a simple "set of vectors" starting with name in
the style of:
$one 1 2
$two 1 2 3 4 5
Is it possible in R?
Many thanks!
Zdenek Skala
[[alternative HTML version deleted]]
what is that you want to do with this data after it is written? you can easily write a function using 'cat' to create any format that you want. Sent from my iPad On Jul 19, 2012, at 5:09, Sk?la, Zden?k (INCOMA GfK)<Zdenek.Skala at gfk.com> wrote:> Dear all, > apologies for this (perhaps recurrent) question but I did not found a question when searching mailing lists. > > How to write a list of a simple kind, e.g.: > > abc <- list(one=(1:2), two=(1:5)) > > # to a file? I understand that write() & co. cannot work but when I try > > sink("aa.txt", append=T, split=T) > abc > sink() > > # the output is indeed "split by rows" in the textfile, each starting with row numbers (and I have rather long "vectors" resulting in many rows...): > > $one > [1] 1 2 > > $two > [1] 1 2 3 4 5 > > What I would need is a simple "set of vectors" starting with name in the style of: > $one 1 2 > $two 1 2 3 4 5 > > Is it possible in R? > > Many thanks! > > Zdenek Skala > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Dear Zdenek
You could generate this file using a loop.
# the data
abc <- list(one=(1:2), two=(1:5))
# create a connection
sink("aa.txt", append=T, split=T)
# for each element in the list, print
for (list_name in names(abc)) {
cat(list_name, unlist(abc[list_name]),"\n") }
# close connection
sink()
# here is another way that might be useful
# unlist the data
aa <- sapply(abc, paste, collapse = " ")
# generate the strings you need
bb <- paste("$", names(abc), " ", aa, sep = "")
# write out data
write.table(bb, "aa.txt", col.names = FALSE, row.names = FALSE, quote
= FALSE)
# hope this helps
Best wishes
Chris
Chris Campbell
Mango Solutions
Data Analysis the Delivers
http://www.mango-solutions.com
+44 (0) 1249 705 450
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Sk?la, Zdenek (INCOMA GfK)
Sent: 19 July 2012 10:10
To: r-help at r-project.org
Subject: [R] write list to ascii
Dear all,
apologies for this (perhaps recurrent) question but I did not found a question
when searching mailing lists.
How to write a list of a simple kind, e.g.:
abc <- list(one=(1:2), two=(1:5))
# to a file? I understand that write() & co. cannot work but when I try
sink("aa.txt", append=T, split=T)
abc
sink()
# the output is indeed "split by rows" in the textfile, each starting
with row numbers (and I have rather long "vectors" resulting in many
rows...):
$one
[1] 1 2
$two
[1] 1 2 3 4 5
What I would need is a simple "set of vectors" starting with name in
the style of:
$one 1 2
$two 1 2 3 4 5
Is it possible in R?
Many thanks!
Zdenek Skala
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
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.
--
LEGAL NOTICE\ \ This message is intended for the use of ...{{dropped:18}}
You will probably need to write a custom function, but it could
use the built-in write.dcf() or formatDL() functions. E.g.,
> # options(width=50)
> write.dcf(list(One=paste(1:50,collapse=" "),
Two=paste(state.abb[1:5], collapse=", ")))
One: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50
Two: AL, AK, AZ, AR, CA
> fx1 <- formatDL(c("One", "Two"),
c(paste(1:50,collapse=" "), paste(state.abb[1:5], collapse=",
")))
> cat(fx1, sep="\n")
One 1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43
44 45 46 47 48 49 50
Two AL, AK, AZ, AR, CA
> fx2 <- formatDL(c("One", "Two"),
c(paste(1:50,collapse=" "), paste(state.abb[1:5], collapse=",
")), style="list")
> cat(fx2, sep="\n")
One: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39 40 41 42
43 44 45 46 47 48 49 50
Two: AL, AK, AZ, AR, CA
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at
r-project.org] On
> Behalf Of Sk?la, Zdenek (INCOMA GfK)
> Sent: Thursday, July 19, 2012 2:10 AM
> To: r-help at r-project.org
> Subject: [R] write list to ascii
>
> Dear all,
> apologies for this (perhaps recurrent) question but I did not found a
question when
> searching mailing lists.
>
> How to write a list of a simple kind, e.g.:
>
> abc <- list(one=(1:2), two=(1:5))
>
> # to a file? I understand that write() & co. cannot work but when I try
>
> sink("aa.txt", append=T, split=T)
> abc
> sink()
>
> # the output is indeed "split by rows" in the textfile, each
starting with row numbers
> (and I have rather long "vectors" resulting in many rows...):
>
> $one
> [1] 1 2
>
> $two
> [1] 1 2 3 4 5
>
> What I would need is a simple "set of vectors" starting with name
in the style of:
> $one 1 2
> $two 1 2 3 4 5
>
> Is it possible in R?
>
> Many thanks!
>
> Zdenek Skala
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.