David Lyon
2013-Apr-02 04:13 UTC
[R] please help, iteration through a list of files and plot each one
I have many files in 1 directory, file names end in .txt. Each file has 2 columns col1 col2 2 3 3 4 4 5 5 6 I want to make a list of the file names and iterate through each plotting them in a separate file $filename\.png with the png swapped for txt. So far I have this, can someone help fill in the blanks? Thank You! file_list <- list.files() ? for (file in file_list){ ?????? ? ? if (!exists("dataset")){ ??? dataset <- read.table(file, header=TRUE, sep="\t") ? } ?? ? ? if (exists("dataset")){ ??? temp_dataset <-read.table(file, header=TRUE, sep="\t") ?######how? to plot(temp_dataset) each file and save png(file\.png) ? ? } ? }
arun
2013-Apr-02 06:05 UTC
[R] please help, iteration through a list of files and plot each one
Hi, May be this helps. list.files() #[1] "file1.txt" "file2.txt" "file3.txt" ?lapply(list.files(),function(x) {x1<-read.table(x,header=TRUE);x2<-gsub("txt","png",x);png(x2);plot(col2~col1,data=x1,type="l");dev.off()}) A.K. ----- Original Message ----- From: David Lyon <david_lyon3 at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, April 2, 2013 12:13 AM Subject: [R] please help, iteration through a list of files and plot each one I have many files in 1 directory, file names end in .txt. Each file has 2 columns col1 col2 2 3 3 4 4 5 5 6 I want to make a list of the file names and iterate through each plotting them in a separate file $filename\.png with the png swapped for txt. So far I have this, can someone help fill in the blanks? Thank You! file_list <- list.files() ? for (file in file_list){ ?????? ? ? if (!exists("dataset")){ ??? dataset <- read.table(file, header=TRUE, sep="\t") ? } ?? ? ? if (exists("dataset")){ ??? temp_dataset <-read.table(file, header=TRUE, sep="\t") ?######how? to plot(temp_dataset) each file and save png(file\.png) ? ? } ? } ______________________________________________ R-help at r-project.org 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.
Shane McMahon
2013-Apr-02 06:12 UTC
[R] please help, iteration through a list of files and plot each one
There's probably a better way to do it, but here's one way: require(stringr) windows() file_list <- list.files(pattern=".txt") for (file in file_list) { dataset <- read.table(file, header=TRUE, sep="\t") plot(dataset) savePlot(filename=str_replace(file,".txt",""),type="png") } On 4/1/2013 11:13 PM, David Lyon wrote:> > I have many files in 1 directory, file names end in .txt. > > Each file has 2 columns > > col1 col2 > > 2 3 > > 3 4 > > 4 5 > > 5 6 > > > I want to make a list of the file names and iterate through each plotting them in a separate file $filename\.png with the png swapped for txt. > > So far I have this, can someone help fill in the blanks? > > Thank You! > > > > > file_list <- list.files() > > for (file in file_list){ > > > if (!exists("dataset")){ > dataset <- read.table(file, header=TRUE, sep="\t") > } > > > if (exists("dataset")){ > temp_dataset <-read.table(file, header=TRUE, sep="\t") > > > ######how to plot(temp_dataset) each file and save png(file\.png) > > } > > } > > ______________________________________________ > R-help at r-project.org 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.