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]]
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.
Yes, already looked at the FAQ. Thats how I have got where I am. I didnt see a solution to this particular problem on there though Thanks anyway. Uwe Ligges <ligges@statistik.uni-dortmund.de> wrote: 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@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.[[alternative HTML version deleted]]
R won't do variable interpolation inside quotation marks as perl does. You could try amending your code with, for e.g. file.name<-paste(sep="/","data_files",files[[i]]) x<-read.table(file.name) Regards, Mike On 9/4/06, Ffenics <ffenics2002 at yahoo.co.uk> 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. >-- Regards, Mike Nielsen