我们可以使用rep并添加初始向量
v1 + rep(0:3, each = length(v1))
#[1] 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7
Run Code Online (Sandbox Code Playgroud)
或使用 sapply
c(sapply(v1, `+`, 0:3))
Run Code Online (Sandbox Code Playgroud)
或使用 outer
c(outer(v1, 0:3, `+`))
Run Code Online (Sandbox Code Playgroud)
v1 <- 1:4
Run Code Online (Sandbox Code Playgroud)