Building on Mr Flick answer:
df$var2 <- cumsum(c(0,as.numeric(diff(df$var1))!=0))
But if you don’t want to use diff
you can still use:
df$var2 <- c(0,cumsum(as.numeric(with(df,var1[1:(length(var1)-1)] != var1[2:length(var1)]))))
It starts at 0, not at 1 but I’m sure you see how to change it if you want to.