[R] opening files in directory
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.
Re: [R] opening files in directory
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. __ 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.
Re: [R] opening files in directory
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 [EMAIL PROTECTED] 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-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.
Re: [R] opening files in directory
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 [EMAIL PROTECTED] 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. -- Regards, Mike Nielsen __ 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.
Re: [R] opening files in directory
On 9/4/06, Mike Nielsen [EMAIL PROTECTED] wrote: R won't do variable interpolation inside quotation marks as perl does. Just as an aside, gsubfn in package gsubfn will do perl-style (well, sort of) string interpolation: library(gsubfn) i - 1 gsubfn(x = data_files/file$i) [1] data_files/file1 cati and cati0 in the same package provide for such interpolation within cat. __ 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.