Error message saying that the .i variable isnt ascending even though it is in tidyverse/slider

So my problem is that I'm trying to do a moving average that with the function slide_index_dbl.
The data I have that I'll use do get a index is:
The .i is variable created : {Date[1:35585], format: "2018-07-10" .}
The .x is the variable of {price: num[1:35585], format: 64900...}
The arrange and group_by variable used is {segment: chr[1:35585], format:"A" "B" "C"..."SUV"}

The code I'm running is:

dv_r <- dvdate %>% select(c(created, distance, price, segment) %>%
 arrange(segment, created) %>% 
group_by(segment) %>% 
mutate(avg90=slide_index_dbl( .x = price, .i = created, .f = ~mean(.x, na.rm = FALSE), .before = days(90)))

I have my variables(segment and created) in ascending order but the error message is still:

Error in `stop_slider()`:
! `.i` must be in ascending order.
i It is not ascending at locations: 33401, 27747, 27751, 33403,....

Any reason for this? Is my date variable wrong or what could it be?

Any help or guidens to a solution is of much help

Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

Welcome to Rstudio community.

First you have to use the triple ticks correctly. They are back ticks like these ``` and not '''

Practice with reprex guide as advised by @nigrahamuk. We will help you if you get stuck.

Keep asking.


Thanks for the advice, I have updated the text. The data seemed to large for dput() function but I explained the variables that is used in the function.
The problem seem to be that when I try to get a rolling average from each car segment by using the "arrange()" function with the variables segment and created in doesnt work, and then I get the error message: .i must be in ascending order.

But when I only arrange by created I works

Thanks for the advice, Ive updated the text. And explained it a bit more in the message above

Which R package has the function slide_index_dbl? Please include the library(...) line at the top so that anyone can run the code by copy pasting on their R console.
BTW if just rolling average is needed why use such a complicated function? Have you tried rollmean from zoo package or frollmean from data.table() ?

The package is "slider".
The problem with the other functions only takes the n integer vector. But I'm looking to take all the observations during a time period as 90days. The data can have multiple sales during one day and also no sales during multiple days.
So if I take the n-vector I might get a lot of sales random dates and not specificly 90days for example. Do you understand what Im trying to explain?
Im willing to use a different function but havent found any with the date as a vector and not integer vector

I see. That is a programming challenge. It can be solved with a little extra coding. But I have not used slider package so let's first wait for someone else to help. Meanwhile as suggested, use reprex package to generate your representative example to be copy pasted. I may be able to help you with the code if you do that.

Ok, in lieu of a reprex from you, lets do detective work.
Here is a simple scenario I made that at least reproduces the error message, even though it may not be relevant to your data.

(slidable <- structure(list(seg = c("a", "a", "a", "b", "b", "b"),
                            d = structure(c(19108, 19109, 19110, 19108, 19109, 19110), class = "Date"),
                            v = 1:6), row.names = c(NA, -6L), class = "data.frame"))
slidable %>% group_by(seg) %>% mutate(
  avg_v =slide_index_dbl( .x = v,
                          .i = d,
                          .f = ~mean(.x, na.rm = FALSE), 
                          .before = days(2)))

# ijust reversed the dates for seg b
(unslidable <- structure(list(seg = c("a", "a", "a", "b", "b", "b"),
                                           d = structure(c(19108, 19109, 19110, 19110, 19109, 19108), class = "Date"),
                                           v = 1:6), row.names = c(NA, -6L), class = "data.frame"))

unslidable %>% group_by(seg) %>% mutate(
  avg_v =slide_index_dbl( .x = v,
                          .i = d,
                          .f = ~mean(.x, na.rm = FALSE), 
                          .before = days(2)))

I learn from this that

error in `stop_slider()`:
! `.i` must be in ascending order.
i It is not ascending at locations: 3, 2.

at least in my example is only the last part of a longer and more helpful error message, which in full is

Error in `mutate()`:
! Problem while computing `avg_v = slide_index_dbl(...)`.
i The error occurred in group 2: seg = "b".
Caused by error in `stop_slider()`:
! `.i` must be in ascending order.
i It is not ascending at locations: 3, 2.
Run `rlang::last_error()` to see where the error occurred

This is helpful as it identifies the segment 'b' as the problematic one, it therefore gives context to locations 3,2 as these relate to the in segment locations.
Do you have a larger error message in your real issue that we can draw information from ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.