[R] write list to ascii

Chris Campbell ccampbell at mango-solutions.com
Thu Jul 19 16:10:46 CEST 2012


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}}



More information about the R-help mailing list