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)] -
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
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
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
4 matches
Mail list logo