-1

One of the columns in my dataset is "Movement_Stats", it contains "forward", "backward" and "Stop". Each row represents an image frame. So this column looks like: "forward, forward, forward, backward, forward, forward...". I want to smooth the categorical values of this column by the rule:

  1. For each row, check its previous 5 rows and next 5 rows (its neighbor)
  2. Re-assign the value of this row by the MAJORITY VOTE of its neighbor

I did not find any package I can use in R.

Yiyang
  • 39
  • 11

1 Answers1

2

You can use rollapply from package zoo together with table:

mov <- c("forward", "backward", "stop")
s <- sample(mov, 1000, replace = TRUE)

zoo::rollapply(s,11, function(x) names(which.max(table(x))))
HubertL
  • 19,246
  • 3
  • 32
  • 51