[R] Iterate over a list of input files?
Mark Knecht
markknecht at gmail.com
Wed Feb 9 01:04:31 CET 2011
Hi,
I've got the following code which seems to work fine for a single
file if I specify the file name explicitly in the code. What I need to
do is run it on all the files in the directory tested and augment the
data frame I'm building to have more results columns.How can I do
that?
Here's the code:
library(chron) # .Holidays / is.holiday / is.weekend
TStoDate = function (TSDate) {
X = strptime(TSDate + 19e6L, "%Y%m%d")
return(as.Date(X))
}
FirstDate = 1090601
LastDate = 1101101
StartDate = TStoDate(FirstDate)
EndDate = TStoDate(LastDate)
# Create a sequence of days from start to finish
# Then remove weekends and holidays and turn into a data.frame
dd <- seq(StartDate, EndDate, by = "day")
TradingDate <- dd[!is.weekend(dd) & !is.holiday(dd)]
DateRange <- as.data.frame(TradingDate)
# Read list of files to run tests on
Files = dir("C:\\CorrelationTests", pattern = "^[T]", full.names=TRUE)
# Create a data.frame with the
A = read.csv(Files[1], header=FALSE)[,c(1,7)]
A[,1] = as.Date(A[,1], format="%m/%d/%Y")
ASum <- with(A,aggregate(V7 ~ V1, FUN = sum))
colnames(ASum) = c("Date","PL")
# Merge the two data frames
Results1 <- merge(DateRange , ASum, by.x = "TradingDate", by.y =
"Date", all.x=TRUE)
Results1$PL[is.na(Results1$PL)] <- 0
DateRange[1:10,]
A[1:10,]
ASum[1:10,]
Results1[1:20,]
Files
It produces this list of results: (file list trimmed)
> DateRange[1:10,]
[1] "2009-06-01" "2009-06-02" "2009-06-03" "2009-06-04" "2009-06-05"
"2009-06-08"
[7] "2009-06-09" "2009-06-10" "2009-06-11" "2009-06-12"
> A[1:10,]
V1 V7
1 2009-06-10 91
2 2009-06-15 -279
3 2009-06-15 861
4 2009-06-22 771
5 2009-06-22 -179
6 2009-06-24 61
7 2009-07-02 491
8 2009-07-06 81
9 2009-07-13 1681
10 2009-08-06 71
> ASum[1:10,]
Date PL
1 2009-06-10 91
2 2009-06-15 582
3 2009-06-22 592
4 2009-06-24 61
5 2009-07-02 491
6 2009-07-06 81
7 2009-07-13 1681
8 2009-08-06 71
9 2009-08-19 1271
10 2009-08-27 601
> Results1[1:20,]
TradingDate PL
1 2009-06-01 0
2 2009-06-02 0
3 2009-06-03 0
4 2009-06-04 0
5 2009-06-05 0
6 2009-06-08 0
7 2009-06-09 0
8 2009-06-10 91
9 2009-06-11 0
10 2009-06-12 0
11 2009-06-15 582
12 2009-06-16 0
13 2009-06-17 0
14 2009-06-18 0
15 2009-06-19 0
16 2009-06-22 592
17 2009-06-23 0
18 2009-06-24 61
19 2009-06-25 0
20 2009-06-26 0
>
> Files
[1] "C:\\CorrelationTests/TF.D-17M-2009_06-2010_11-V1.csv"
[2] "C:\\CorrelationTests/TF.D-17M-2009_06-2010_11-V2.csv"
[3] "C:\\CorrelationTests/TF.D-17M-2009_06-2010_11-V3.csv"
[4] "C:\\CorrelationTests/TF.D-17M-2009_06-2010_11-V4.csv"
[5] "C:\\CorrelationTests/TF.D-17M-2009_06-2010_11-V5.csv"
<SNIP>
>
I don't know how to go about iterating through the file list adding a
new column to the Results1 data frame as I go.
How can I go about doing that?
Thanks,
Mark
More information about the R-help
mailing list