[R] Recall: Problem with sequence in loop
Muhammad Rahiz
muhammad.rahiz at ouce.ox.ac.uk
Sun Dec 20 18:42:08 CET 2009
I recall the above problem. It works. Just error on my part in the file
output.
Here is the script that works.
-----
seq <- paste(seq(1914, 1916, by=1), "*.y", sep=".")
for (i in 1:3){
list <- list.files("~/ukcp09/txt/x.djf", seq[[i]])
file <- lapply(list, read.table)
mean <- (Reduce("+", file))
myfile1 <- paste(strsplit(seq[[i]], "\\.")[[1]][1], "sum.txt", sep=".")
write.table(mean, file=myfile1, row.names=FALSE, col.names=FALSE)
}
-----
Muhammad Rahiz wrote:
> Hi all,
>
> I've got a list of files from 1914 to 2000. For each file, I can call the read.table function as follows.
>
>
>> file <- read.table("1914.txt")
>>
>
>
> But I want to make a script so that at the end of the loop, the script selects the file 1915.txt, 1916.txt and so on.
>
> I've used seq() to create the increment
>
> seq <- paste(seq(1914, 2006, by=1), "*.txt.h", sep=".")
>
> But it didn't work.
>
> Below is the script;
>
> seq <- paste(seq(1914, 2006, by=1), "*.txt", sep=".")
>
> for (i in 1:93){
> list <- list.files("~/ukcp09/txt/x.djf", seq)
> file <- lapply(list, read.table)
>
> mean <- (Reduce("+", file))/3
> }
>
> When I call
>
> > objects()
> > list
> [1] "2006.01.txt.h" "2006.02.txt.h" "2006.12.txt.h"
>
> The listed files are the last files in the sequence.
>
> Am I doing the loop correctly?
>
>
More information about the R-help
mailing list