[R] Problem

Thomas Lumley tlumley at u.washington.edu
Wed May 8 21:19:42 CEST 2002


On Wed, 8 May 2002, Tomei Lodino wrote:

> Hi! This is the situation.
> I have these commands. I have not to change anything in these lines but I
> write them to make you understand the situation.
>
> m1<-read.table("data",fill=TRUE,header=FALSE)
> m2<-read.table("query.txt)
> qvals<-levels(m2[[3]])
> m1<-m1[m1[,1]%in%qvals,]
> relnodes<-apply(m1,1,function(x)sum(x%in%qvals))
> m1<-m1[relnodes>1,]
> m1b<-as.list(as.data.frame(t(as.matrix(m1))))
> m1c<-lapply(m1b,function(x)as.character(x[x%in%qvals]))
>
> These lines import the external file "data", they make some things and give
> me a output.
> Now my problem is that  I have more or less 100 files. Everytime I take a
> file, I call it m1 and I go on. When I obtain an output, a write
> unione<-(unione,m1c) to update the output given before. (before starting the
> first time I do unione<-NULL)
> Now,  I want to know if there is some commands that repeat all the lines
> written before, taking one by one all the 100 files that I have to use.
> Maybe I can use the "for" command, but in which way?
> Summarizing I have to do 100 times the lines written before, changing
> everytime the name of the external file.
> All the files are in the same directory. The file in m2 is the same for all
> the iterations and so I have not to change it.
> The names of the files are "dataa","datab","datac",...

Something like

all.file.names<-list.files(pattern="data")
 m2<-read.table("query.txt)
 qvals<-levels(m2[[3]])
unione<-NULL

for (filename in all.file.names){
  m1<-read.table(filename,fill=TRUE,header=FALSE)
  m1<-m1[m1[,1]%in%qvals,]
  relnodes<-apply(m1,1,function(x)sum(x%in%qvals))
  m1<-m1[relnodes>1,]
  m1b<-as.list(as.data.frame(t(as.matrix(m1))))
  m1c<-lapply(m1b,function(x)as.character(x[x%in%qvals]))
  unione[[filename]]<-m1c
}
and then if you want to concatenate all the results
  do.call("c", unione)


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list