[R] Help reading table rows into lists

Alison Waller alison.waller at embl.de
Sun Oct 10 17:40:32 CEST 2010


Hi all,

I have a large table mapping thousands of COGs(groups of genes) to  
pathways.
# Ex
COG0001	patha	pathb  	pathc
COG0002	pathd	pathe
COG0003	pathe	pathf	pathg	pathh
##

I would like to combine this information into a big list such as below
COG2PATHWAY<- 
list 
(COG0001 
= 
c 
("patha 
","pathb 
","pathc 
"),COG0002=c("pathd","pathe"),COG0003=c("pathf","pathg","pathh"))

I am stuck and have tried various methods involving (probably mangled)  
versions of lappy and loops.

Any suggestions on the most efficient way to do this would be great.

Thanks,

Alison

Here is my latest attempt.

#####

line_num<-length(scan(file="/g/bork8/waller/ 
test_COGtoPath.txt",what="character",sep="\n"))
COG2Path<-vector("list",line_num)
COG2Path<-lapply(1:(line_num-1),function(x) scan(file="/g/bork8/waller/ 
test_COGtopath.txt",skip=x,nlines=1,quiet=T,what='character',sep="\t"))

#####

I am getting an error

#####

 >COG2Path<-lapply(1:(line_num-1),function(x) scan(file="/g/bork8/ 
waller/ 
test_COGtopath.txt",skip=x,nlines=1,quiet=T,what='character',sep="\t"))
Error in file(file, "r") : cannot open the connection
In addition: Warning message:
In file(file, "r") :

But if I do scan alone I don't get an error

# then I suppose it looks like the easiest wasy to name the list  
variables is using unix to cut the first column out and then read that  
in.
names(COG2Path)<-scan(file="/g/bork8/waller/ 
test_col_names.txt",sep="\t",what="character")



More information about the R-help mailing list