Lalitha
Lalitha

Reputation: 147

how to find the negative trend in time series

I have a multiple time-series data. I would like to find out the customers that have negative trend.

Data

customer_id    date    sales
A              2018-04-22  2.86
A              2018-04-29  4.39
A              2018-05-06  10.5
B              2018-08-05  10
B              2018-08-12  7.3
B              2018-08-19  8.4




tab <- Data %>% group_by(customer_id) %>% arrange(customer_id,date) %>%
         mutate(new <- last(Data$sales))- first(Data$sales)))

Here I would like to do for every customer and expected output is

Customer_id   new
A             7.64
B             -1.6

So that I will get the customers that have negative trend

Upvotes: 0

Views: 312

Answers (1)

arg0naut91
arg0naut91

Reputation: 14764

You can do:

Data %>% group_by(customer_id) %>% 
  arrange(customer_id, date) %>%
  summarise(new = last(sales)- first(sales))

Output:

# A tibble: 2 x 2
  customer_id   new
  <chr>       <dbl>
1 A            7.64
2 B           -1.60

Upvotes: 2

Related Questions