简体   繁体   English

如何在 while 循环和 append 中将特定数字重复为 R 中的数字

[英]How to repeat specific numbers in while loop and append them as numeric in R

I was wondering if someone help me to solve my problem.我想知道是否有人帮助我解决我的问题。 I want to produce numbers times if I have a number more than 10. For example, if I want to write simple code:如果我的数字超过 10,我想产生数字次。例如,如果我想编写简单的代码:

    n= 53 
mem = rep(c(1,2,3,4,5,6),times=c(10,10,10,10,10,3))
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5  
[50] 5 6 6 6 

n= 52
mem = rep(c(1,2,3,4,5,6),times=c(10,10,10,10,10,2)) 
 
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5  
[50] 5 6 6 

#To produce the numbers with specific repetition, I wrote the While loop. #为了产生具有特定重复的数字,我编写了 While 循环。 however, it #works for 50 or 60 not for 53 or 62. and it doesn't append it.但是,它#适用于 50 或 60,不适用于 53 或 62。它不适用于 append。 Thanks in advance.提前致谢。 #The code is here: #代码在这里:

n=53
x= rep(c(), times= c())
num=0
while (n>10) {
  n=n-10
  num=num+1
  x= rep(c(num), times= c(10))
  n=n+1
  x= rep(c(num), times= c(10))
  print(x)
  df <- rbind(df,as.numeric(x))
}

We can add rep for the times as well我们也可以为times添加rep

n <- 52
rep(1:6, times = rep(c(10, 2), c(5, 1)))
#[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6

n <- 53
rep(1:6, times = rep(c(10, 3), c(5, 1)))
#[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 6 6 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM