[R] please help, iteration through a list of files and plot each one

arun smartpink111 at yahoo.com
Tue Apr 2 08:05:44 CEST 2013


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.




More information about the R-help mailing list