Re: [R] For loop by factor.

2011-06-20 Thread Sarah Goslee
Chris, This doesn't cover all possible cases, but does work for your example. It should be enough for you to tweak for your actual data. diffsum <- function(x) { # first identify the decreasing values # and the difference between increasing values xdif <- x[2:length(x)] -

Re: [R] For loop by factor.

2011-06-19 Thread Bill.Venables
If I understand you correctly, you are trying to find the cumulative maximum from the end within each level of the factor. If this is what you are trying to do, then here is one way you might like to do it. First, define the function: > cumMax <- function(x) Reduce(max, x, right = TRUE, accum

Re: [R] For loop by factor.

2011-06-19 Thread jim holtman
try this: > test <- data.frame(A=c("a", "a", "a", "b", "b", "c", "c", "c", "c"), > B=c(3,2,1,3,2,2,3,1,1)) > test A B 1 a 3 2 a 2 3 a 1 4 b 3 5 b 2 6 c 2 7 c 3 8 c 1 9 c 1 > # determine which group is not decreasing > tapply(test$B, test$A, function(x) any(diff(x) > 0)) a b c FA

Re: [R] For loop by factor.

2011-06-19 Thread Sarah Goslee
This works, but I'm still hunting for a more elegant final step: > test <- data.frame(A=c("a", "a", "a", "b", "b", "c", "c", "c", "c"), > B=c(3,2,1,3,2,2,3,1,1)) > test2 <- lapply(split(test$B, test$A), sort, dec=TRUE) > test3 <- data.frame(A=rep(names(test2), times=lapply(test2, length)), > B=un