[R] opening files in directory

Uwe Ligges ligges at statistik.uni-dortmund.de
Mon Sep 4 18:27:54 CEST 2006


FAQ ...

Uwe Ligges



Ffenics wrote:
> Hi there
> I want to be able to take all the files in a given directory, read them in one at a time, calculate a distance matrix for them (the files are data matrices) and then print them out to separate files. This is the code I thought I would be able to use
> (all files are in directory data_files)
> for(i in 1:length(files))
> + {
> + x<-read.table("data_files/files[[i]]")
> + dist<-dist(x, method="euclidean", diag=TRUE)
> + mat<-as.matrix(dist)
> + write.table(mat, file="files[[i]]")
> + }
> But I get this error when I try to open the first file using read.table
> Error in file(file, "r") : unable to open connection
> In addition: Warning message:
> cannot open file 'data_files/files[[i]]'
> if I try the read.table command without the quotation marks like so
> x<-read.table(data_matrix_files/files[[i]])
> I get the error 
> Error in read.table(data_matrix_files/files[[i]]) :
>         Object "data_matrix_files" not found
> But if I go to the directory where the files are kept before starting up R, the read.table command without the quotation marks works.
> I don't want to start up R in the same directory as the where the files I will be using reside though so how do I rectify this?
> Any help much appreciated
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list