Assuming the material is ordered time already, first define a cumulative excellent function:
dist_cum <- function(var) sapply(seq_along(var), function(x) length(unique(head(var, x))))
Then the basic solution that ave uses to create groups (note that accepts var1 is a factor), and then applies our function to each group:
transform(df, var2=ave(as.integer(var1), grp, FUN=dist_cum))
A data.table , basically doing the same thing:
library(data.table) (data.table(df)[, var2:=dist_cum(var1), by=grp])
And dplyr , again:
library(dplyr) df %>% group_by(grp) %>% mutate(var2=dist_cum(var1))
source share