Muhammad Aamir
Muhammad Aamir

Reputation: 79

Calculating the value to know the trend in a set of numeric values

I have a requirement where I have set of numeric values for example: 2, 4, 2, 5, 0

As we can see in above set of numbers the trend is mixed but since the latest number is 0, I would consider the value is getting DOWN. Is there any way to measure the trend (either it is getting up or down).

Is there any R package available for that?

Thanks

Upvotes: 0

Views: 81

Answers (1)

Amit
Amit

Reputation: 2098

Suppose your vector is c(2, 4, 2, 5, 0) and you want to know last value (increasing, constant or decreasing), then you could use diff function with a lag of 1. Below is an example.

MyVec <- c(2, 4, 2, 5, 0)
Lagged_vec <- diff(MyVec, lag=1)
if(MyVec[length(MyVec)]<0){
   print("Decreasing")}
else if(MyVec[length(MyVec)]==0){
   print("Constant")}
else {print("Increasing")}

Please let me know if this is what you wanted.

Upvotes: 1

Related Questions