生成一个向量,其中 x、y、z 重复 n 次,并具有固定的上限

发布于 2025-01-19 19:55:24 字数 233 浏览 1 评论 0原文

我正在尝试创建一个矢量,其中我有3个重复的数字1,然后是数字2的3个重复,例如,数字36的3个重复。

c(1,1,1,1, 1,2,2,2,3,3,3,3,4,4,4,5,5,5 ...)

我尝试使用以下REP(),但有以下错误:

<代码> REP中的错误(3,seq(1:36)):参数'times'不正确

我需要使用什么公式来正确生成我想要的向量?

I am trying to create a vector where I have 3 repetitions of the number 1, then 3 repetitions of the number 2, and so on up to, for instance, 3 repetitions of the number 36.

c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5...)

I have tried the following use of rep() but got the following error:

Error in rep(3, seq(1:36)) : argument 'times' incorrect

What formulation do I need to use to properly generate the vector I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白首有我共你 2025-01-26 19:55:24
sort(rep(1:36, 3))

或者甚至更好,正如@Wimpel 在评论中提到的那样,使用rep 函数的每个参数。

rep(1:36, each = 3)

输出

# [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9  9  9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22
#  [65] 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36 36
sort(rep(1:36, 3))

Or even better as @Wimpel mentioned in the comments, use the each argument of the rep function.

rep(1:36, each = 3)

output

# [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9  9  9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22
#  [65] 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34 34 34 35 35 35 36 36 36
攒眉千度 2025-01-26 19:55:24

这个应该起作用。但是可能不是最优雅的。

reps = c()
n = 36
for(i in 1:n){
  reps = append(reps, rep(i, 3))
}
reps

或者使用重复函数正确(请参阅文档(rep?rep)有关参数每个):

rep(1:36,each = 3)

This one should work. However probably not the most elegant.

reps = c()
n = 36
for(i in 1:n){
  reps = append(reps, rep(i, 3))
}
reps

alternatively using the rep function properly (see documentation (?rep for argument each):

rep(1:36,each = 3)
荆棘i 2025-01-26 19:55:24

rep方法是可取的(请参阅现有答案),

这里还有其他选项:

> kronecker(1:36, rep(1, 3))
  [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9
 [26]  9  9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17
 [51] 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
 [76] 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34
[101] 34 34 35 35 35 36 36 36

> c(outer(rep(1, 3), 1:36))
  [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9
 [26]  9  9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17
 [51] 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
 [76] 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34
[101] 34 34 35 35 35 36 36 36

rep approach is preferable (see existing answers)

Here are some other options:

> kronecker(1:36, rep(1, 3))
  [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9
 [26]  9  9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17
 [51] 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
 [76] 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34
[101] 34 34 35 35 35 36 36 36

> c(outer(rep(1, 3), 1:36))
  [1]  1  1  1  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9
 [26]  9  9 10 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 17 17
 [51] 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25
 [76] 26 26 26 27 27 27 28 28 28 29 29 29 30 30 30 31 31 31 32 32 32 33 33 33 34
[101] 34 34 35 35 35 36 36 36
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文