Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的 R 课有一个工作簿问题,我不知道。我需要“编写一个 R 命令,使用 rep() 创建一个包含元素 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7 的向量”
它似乎是一个 1 到 4 的重复序列,重复 4 次,并且在每次重复时将 1 添加到起始元素。我对 R 非常陌生,所以我很难过。任何帮助,将不胜感激。
我们可以使用rep和添加初始向量
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
或使用sapply
sapply
c(sapply(v1, `+`, 0:3))
或使用outer
outer
c(outer(v1, 0:3, `+`))
v1 <- 1:4