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)] -
n...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Christopher Peters
Sent: Monday, 20 June 2011 6:21 AM
To: r-help@r-project.org
Subject: [R] For loop by factor.
I have a data.frame as follows:
a 3
a 2
a 1
b 3
b 2
c 2
c 3
c 1
c 1
Each factor (a, b, c) should be monotonically
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
I have a data.frame as follows:
a 3
a 2
a 1
b 3
b 2
c 2
c 3
c 1
c 1
Each factor (a, b, c) should be monotonically decreasing, notice that factor
'c' is not.
I could use some help to figure out how to form a logical structure (mostly
just syntax), that will check each 'next value' for ea
5 matches
Mail list logo