I have created a dataframe that is alike to yours, for ease of explanation, try to provide a reproducible example yourself next time :-).
df <- data.frame( "id" = c(1,2,3,4,5,5,5,5,6,6,6,6), # id values
"month" = c(6,7,8,9,6,7,8, 9,6,7,8,9), # month values
"outstanding" = c(500, 300, 30, 40, 56, 32, 35, 40, 56, 32, 32, 59), #outstanding amount
"paid" = c("n", "y", "y", "y", "y", "y","n", "y", "y", "n", "n", "y"), # paid yes or no
"state" = '', #state: filling next
stringsAsFactors = FALSE # not having characters as factors, not really needed here.
)
This dataframe is grouped on id and than mutated by using the lag
function from dplyr. This function compares, within your grouping, the current value with the value above. lead
does the opposite.
df %>%
group_by( id) %>% #group observations by their id
mutate( state = ifelse( paid == lag(paid), "stable", "change")) # if the current observations, grouped by id, differs from the previous (lagged), we observe a change.
Note that for your data, it might be needed to transform your month of payment to an ordered factor or date, to correctly find a change in state