Hi, I have a data.frame that is date ordered by row number - earliest date first and most current last. I want to create a couple of new columns that show the max and min values from other columns *so far* - not for the whole data.frame.
It seems this sort of question is really coming from my lack of understanding about how R intends me to limit myself to portions of a data.frame. I get the impression from the help files that the generic way is that if I'm on the 500th row of a 1000 row data.frame and want to limit the search max does to rows 1:500 I should use something like [1:row] but it's not working inside my function. The idea works outside the function, in the sense I can create tempt1[1:7] and the max function returns what I expect. How do I do this with row? Simple example attached. hp should be 'highest p', ll should be 'lowest l'. I get an error message "Error in 1:row : NA/NaN argument" Thanks, Mark AddCols = function (MyFrame) { MyFrame$p<-0 MyFrame$l<-0 MyFrame$pc<-0 MyFrame$lc<-0 MyFrame$pwin<-0 MyFrame$hp<-0 MyFrame$ll<-0 return(MyFrame) } BinPosNeg = function (MyFrame) { ## Positive y in p column, negative y in l column pos <- MyFrame$y > 0 MyFrame$p[pos] <- MyFrame$y[pos] MyFrame$l[!pos] <- MyFrame$y[!pos] return(MyFrame) } RunningCount = function (MyFrame) { ## Running count of p & l events pos <- (MyFrame$p > 0) MyFrame$pc <- cumsum(pos) pos <- (MyFrame$l < 0) MyFrame$lc <- cumsum(pos) return(MyFrame) } PercentWins = function (MyFrame) { MyFrame$pwin <- round((MyFrame$pc / (MyFrame$pc+MyFrame$lc)),2) return(MyFrame) } HighLow = function (MyFrame) { temp1 <- MyFrame$p[1:row] MyFrame$hp <- max(temp1) ## Highest p temp1 <- MyFrame$l[1:row] MyFrame$ll <- min(temp1) ## Lowest l return(MyFrame) } F1 <- data.frame(x=1:10, y=2*(-4:5) ) F1 <- AddCols(F1) F1 <- BinPosNeg(F1) F1 <- RunningCount(F1) F1 <- PercentWins(F1) F1 F1 <- HighLow(F1) F1 temp1<-F1$p[1:5] max(temp1) temp1<-F1$p[1:7] max(temp1) temp1<-F1$p[1:10] max(temp1) ______________________________________________ R-help@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.