Q:

R: value preceding a change

The following code splits test into the numbers following a decrease, increase or no change.
test <- c(1,2,3,4,5,5,4,1,2,3)
ifelse(diff(test)>0, "up", ifelse(diff(test)<0, "down", "no change"))

[1] "up" "up" "up" "up" "no change" "down" "down" "up" "up"  

However, rather than getting the result of a change, I need "up", "down" or "no change" to reflect the number preceding the change. 
For example, the last 1 in the above set would be up, because it's followed by a higher number.
I've tried various lags, but as it doesn't change the logic of the if statement it hasn't worked. Is there a way to do this?
Edit: 
The expected output is similar to the output shown in the example above, but rather than dropping the first number due to it having no predecessor, the last figure is excluded because there is no change following it.
Really hope that made sense, but if not I'll do my best to clarify further.

A:

diff(x) returns an array of length one less than x, so that diff(x)[i]=x[i+1]-x[i].  Whether you want to consider this the change "before i+1" or the change "after i" makes no difference, it's still the same value.
If you want to get back an array of the same length as your input, for your case try either c("no change", result) or c(result, "no change"), which will pad the result to be the same length as the input, and signify either that "no change" occurs before the first entry, or that "no change" occurs after the last entry.  I can't figure out which one of these is the one you want, but it should be one of the two.

