there is a need to apply this filter to the data:

for (j in 1:n_filtra){ for (i in 2:(nrow(data_point1)-1)){ data_point1$x[i] <- (data_point1$x_filtred[i-1] + data_point1$x_filtred[i] + data_point1$x_filtred[i+1])/3 data_point1$y[i] <- (data_point1$y_filtred[i-1] + data_point1$y_filtred[i] + data_point1$y_filtred[i+1])/3 } data_point1$x_filtred <- data_point1$x data_point1$y_filtred <- data_point1$y } 

With such an implementation, the code works very slowly, as far as I understand, through the apply functions, use the element i + 1, i-1 will fail. Are there any other ways to write this code more productive? The number of rows in the data frame is about 20,000, the number of filter iterations n_filtra = 10.

  • one
    Give an example of data or code to generate similar in structure. - Artem Klevtsov

1 answer 1

Try replacing the inner loop with a view construct:

 DF[2:nrow(DF), "x"] <- RcppRoll::roll_mean(DF$x, n = 3) 

For y respectively.