Thursday, 8 August 2013

R for loop not working

R for loop not working

I'm trying to use R to find the max value of each day for 1 to n days. My
issue is there are multiple values in each day. Heres my code. After I run
it incorrect number of dimensions.
Any suggestions:
Days <- unique(theData$Date) #Gets each unique Day
numDays <- length(Days)
Time <- unique(theData$Time) #Gets each unique time
numTime <- length(Time)
rowCnt <- 1
for (i in 1:numDays) #Do something for each individual day. In this case
find max
{
temp <- which(theData[i]$Date == numDays[i])
temp <- theData[[i]][temp,]
High[rowCnt, (i-2)+2] <- max(temp$High) #indexing for when I
print to CSV
rowCnt <- rowCnt + 1
}
Heres what it should come out to: Except 1 to n days and times.
Day Time Value
x 5 0
x 6 1
x 7 2
x 8 3
y 1 12
y 2 0
y 3 1
y 4 5
so this should return:
day time value
x 8 3
y 1 12
Any help would be greatly appreciated! Thanks!

No comments:

Post a Comment