use seq_along(foo) in place of two calls in 1:length(foo)