[R] write list to ascii

Skála, Zdeněk (INCOMA GfK) Zdenek.Skala at gfk.com
Thu Jul 19 16:21:47 CEST 2012

Dear Chris,

many thanks! This is just what I had in mind! (namely the 'sapply' solution).

Thank you and best regards!


-----Original Message-----
From: Chris Campbell [mailto:ccampbell at mango-solutions.com] 
Sent: Thursday, July 19, 2012 4:11 PM
To: Skála, Zdeněk (INCOMA GfK)
Cc: r-help at r-project.org; Consultants
Subject: RE: write list to ascii

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        


# 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 Campbell
Mango Solutions
Data Analysis the Delivers
+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)

# 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...):

[1] 1 2

[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
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:13}}

More information about the R-help mailing list