[R] Problem

Jason Turner jasont at indigoindustrial.co.nz
Wed May 8 21:22:05 CEST 2002

On Wed, May 08, 2002 at 07:14:27PM +0200, Tomei Lodino wrote:
> Hi! This is 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]))
> 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)

I'm not sure what unione <- (unione,m1c) was meant to be, but it's
a syntax error on my system.  list(unione,m1c), perhaps?

> 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",...

Easiest way is to get the names of the data files in on long
vector, and do your loop over that.

If you run this command in the directory with the data files, try 
something like...

my.data.files <- list.files(pattern="^data")
for(fl in my.data.files) {
	unione <- list(unione,m1c)

(assuming "list" was what you meant)

The pattern="^data" meas "file names starting with 'data'".

There are other ways.  You could also try

my.data.files <- list.files(pattern="^data")
unione <- apply(as.matrix(my.data.files),1,
	function(fl,...) {

This will make a list for you.  I find the *apply functions
very handy for that reason (among many others) - the creation and 
bookkeeping of data structures is done for you.

You should also read the help page for "try" - in case some of
the data files aren't really data files (bad names, corrupted, etc).
This way, the loop won't stop if you encounter an error with
read.table on one file

Indigo Industrial Controls Ltd.
jasont at indigoindustrial.co.nz
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