use seq_along(x) instead of 1:length(x)