Skip to content

Writing a list help

3 messages · Rajasekaramya, Greg Snow, Jagat.K.Sheth at wellsfargo.com

#
hi there,
I have a list called MyList.

MyList[[1]]
[1] "SRY"                                                                        
 [2] "RPS4Y1"                                                                     
 [3] "ZFY"                                                                        
 [4] "ZFX /// ZFY"                                                                
 [5] "LOC728137"                                                                  
 [6] "LOC100101116 /// TTTY1"                                                     
 [7] "AMELY"                                                                      
 [8] "TBL1Y"                                                                      
 [9] "PRKY"           
[[2]]
.
.
.
[[24]]

I have list.I wanna write it to file.

for(i in 1:24)
{
write.table(as.vector(as.matrix(MyLIST[[i]]
file="geneset.txt",sep="\t",append=T) 
}

I used a for loop to write my list.It worked but i have two concerns with
it.

First while writing in a txt file it writing in the names verticaly as it
appears above but i want it to in row
it should look like
SRY" RPS4Y1" ZFY" ZFX /// ZFY" AMELY"  TBL1Y"   PRKY"   

second is i want the list name as my row name.

I tried some thing like row.names=names(mylist[[i]]) but it didnit work.

kindly let me know ur suggestions as how to go about it.

Regards
Ramya
#
What are you planning to do with the file after it is created?

The best way depends on what you want to do.  The write.table function may not be the best choice, there are several functions for writing data to files including dump, write, cat, save, and probably others, some of these will write the entire list out in one step, some are more human readable than others (but the others may be more easily read back into R or into another program).

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
#
You can get all the rownames by calling write.table once on something
like

# if all list elements have equal length
t(data.frame(MyList))
# otherwise, let NA extend where needed
t(do.call(cbind, lapply(MyList, ts))) 

You can take care of NAs as needed on your end. 

HTH 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Rajasekaramya
Sent: Thursday, December 04, 2008 10:52 AM
To: r-help at r-project.org
Subject: [R] Writing a list help


hi there,
I have a list called MyList.

MyList[[1]]
[1] "SRY"

 [2] "RPS4Y1"

 [3] "ZFY"

 [4] "ZFX /// ZFY"

 [5] "LOC728137"

 [6] "LOC100101116 /// TTTY1"

 [7] "AMELY"

 [8] "TBL1Y"

 [9] "PRKY"           
[[2]]
.
.
.
[[24]]

I have list.I wanna write it to file.

for(i in 1:24)
{
write.table(as.vector(as.matrix(MyLIST[[i]]
file="geneset.txt",sep="\t",append=T) 
}

I used a for loop to write my list.It worked but i have two concerns
with
it.

First while writing in a txt file it writing in the names verticaly as
it
appears above but i want it to in row
it should look like
SRY" RPS4Y1" ZFY" ZFX /// ZFY" AMELY"  TBL1Y"   PRKY"   

second is i want the list name as my row name.

I tried some thing like row.names=names(mylist[[i]]) but it didnit work.

kindly let me know ur suggestions as how to go about it.

Regards
Ramya