Search code examples
rcategorical-datasmoothing

Smooth a sequence of Categorical Data in R


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.


Solution

  • 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))))