[R-sig-eco] for loop - list.files()

Daniel Gruner dsgruner at umd.edu
Wed Sep 23 05:36:14 CEST 2009


Hi:

I received several responses which did not quite fix the issue but 
encouraged me to play around with the working directory (not included 
in the first set). It turns out that the "setwd()" line seems 
necessary, as does the same path for the "list.files()" line. Many 
thanks for the responses which stimulated the working fix.



setwd("C:/User/Sims")
result <- matrix(0,nrow=17,ncol=1)  #temp matrix for each 1:1000 result
Sims <- list.files(path="C:/User/Sims", pattern = "^[Sim]")  #this 
includes only files beginning with "Sim"

for (i in 1:length(Sims)) {
tmp<-read.table(Sims[i],row.names=1,header=T, sep=" ")
incidence <- rowSums(tmp)
for (j in 1:ncol(tmp)) {
         for (i in 1:nrow(tmp)) {
                 if((tmp[i,j] == 1) & (incidence[i] == 1)) 
result[j,1] <- result[j,1] + 1.0
                }
        }
cat(t(result), file = "output.txt", sep = " ", fill = TRUE,
          append = TRUE)
result <- matrix(0,nrow=17,ncol=1)
}



At 06:31 PM 9/22/2009, Daniel Gruner wrote:
>Hi:
>
>Back in 2007 I wrote code to loop through & open 1000 separate text 
>files (species-sample incidence (0,1) matrices, from 1000 null model 
>simulation runs), extract key information from each of the files, 
>and paste append each result as a row in an output matrix.
>
>This worked fine in 2007, but for some reason it is not working now. 
>I wonder if there have been some changes in the commands I use in 
>the chunk, such that I now have to modify the code accordingly. I am 
>not attaching data because I would need to send along a bunch of txt 
>files to place in a directory, but if you are interested in running 
>this I can send everything to you (or a manageable subset).
>
>The error I receive is:
>"Error in file(file, "r") : cannot open the connection
>In addition: Warning message:
>In file(file, "r") : cannot open file 'Sim1.txt': No such file or directory"
>
>And here is the code (again, it worked just fine for multiple users 
>in 2007!). I get the same error when I run just the read.table() 
>line but for what reason I cannot divine.
>
>###
># this creates a character string of all the 1000 text files stored 
>in a directory, alone, with no other files
>PlantSims <- list.files(path="C:/user/directory")
># create temporary matrix that to store the summary data from 17 
>islands in each run; clear it (below) for next run
>result <- matrix(0,nrow=17,ncol=1)
>
># loop through and open each of the text files in the list 
>"PlantSims", calculate, drop result for
># 17 islands as 1 of 1000 rows in file="output.txt"
># "incidence" is the total no. islands (of 17) a species is found
>for (i in 1:length(PlantSims)) {
>tmp<-read.table(PlantSims[i],row.names=1,header=T, sep=" ")
>incidence <- rowSums(tmp)
>for (j in 1:ncol(tmp)) {
>         for (i in 1:nrow(tmp)) {
>                 if((tmp[i,j] == 1) & (incidence[i] == 1)) 
> result[j,1] <- result[j,1] + 1.0
>                }
>        }
>cat(t(result), file = "output.txt", sep = " ", fill = TRUE, labels = 
>FALSE, append = TRUE)
>result <- matrix(0,nrow=17,ncol=1)
>}
>###
>
>What might be wrong with this loop, specifically, with the 
>read.table() statement, and is there a better way? The first 
>"PlantSims" statement works correctly, so I do not know why 
>read.table() cannot handle it within the loop.
>
>Thanks very much in advance,
>
>
>Daniel S. Gruner, Assistant Professor
>Department of Entomology
>4112 Plant Sciences Bldg
>University of Maryland
>College Park, MD 20742 U.S.A.
>(o) 301-405-3957  (f) 301-314-9290
>dsgruner at umd.edu
>
>http://grunerlab.umd.edu
>
>_______________________________________________
>R-sig-ecology mailing list
>R-sig-ecology at r-project.org
>https://stat.ethz.ch/mailman/listinfo/r-sig-ecology



More information about the R-sig-ecology mailing list