Dear Contributors I am trying to create a numeric series with repeated numbers, not difficult task, but I do not seem to find an efficient way. This is my solution blocB <- c(rep(x = 1, times = 84), rep(x = 2, times = 84), rep(x = 3, times = 84), rep(x = 4, times = 84), rep(x = 5, times = 84), rep(x = 6, times 84), rep(x = 7, times = 84), rep(x = 8, times = 84), rep(x = 9, times 84), rep(x = 10, times = 84), rep(x = 11, times = 84), rep(x = 12, times 84), rep(x = 13, times = 84)) which works but it is super silly and I need to create different variables similar to this, changing the value of the repetition, 84 in this case. Thanks for any help. F. [[alternative HTML version deleted]]
I apologize, I solved the problem, sorry for that. f. Il giorno gio 13 giu 2024 alle ore 16:42 Francesca PANCOTTO < francesca.pancotto at unimore.it> ha scritto:> Dear Contributors > I am trying to create a numeric series with repeated numbers, not > difficult task, but I do not seem to find an efficient way. > > This is my solution > > blocB <- c(rep(x = 1, times = 84), rep(x = 2, times = 84), rep(x = 3, > times = 84), rep(x = 4, times = 84), rep(x = 5, times = 84), rep(x = 6, > times = 84), rep(x = 7, times = 84), rep(x = 8, times = 84), rep(x = 9, > times = 84), rep(x = 10, times = 84), rep(x = 11, times = 84), rep(x = 12, > times = 84), rep(x = 13, times = 84)) > > which works but it is super silly and I need to create different variables > similar to this, changing the value of the repetition, 84 in this case. > Thanks for any help. > > > F. >[[alternative HTML version deleted]]
@vi@e@gross m@iii@g oii gm@ii@com
2024-Jun-14 02:03 UTC
[R] Create a numeric series in an efficient way
For the particular example you asked for, consider the "each" you can use with rep() rep(1:13, each=84) This is what it does for a shorter version of 4 each:> rep(1:13, each=4)[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 [41] 11 11 11 11 12 12 12 12 13 13 13 13 For another variant, make 84 copies of 1:13 and sort that as you happen to want the numbers in order. sort(rep(1:13, each=84)) The output is the same. If you want a much more compact solution that handles arbitrary pairs of "what to copy", number_of_copies, you can write a function that evaluates two arguments at a time or takes two vectors as arguments like this one I wrote quickly and crudely: rep_many <- function(items, counts) { result <- c() for (index in 1:length(items)) { result <- c(result, rep(items[index], counts[index])) } return(result) } rep_many(1:13, rep(84,13)) The same ideas can be used using a data.frame or functional programming methods but the above is simple enough to flexibly create two vectors specifying how much of each. You said you found a solution, so you may want to share what you chose already. -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Francesca PANCOTTO via R-help Sent: Thursday, June 13, 2024 10:42 AM To: r-help at r-project.org Subject: [R] Create a numeric series in an efficient way Dear Contributors I am trying to create a numeric series with repeated numbers, not difficult task, but I do not seem to find an efficient way. This is my solution blocB <- c(rep(x = 1, times = 84), rep(x = 2, times = 84), rep(x = 3, times = 84), rep(x = 4, times = 84), rep(x = 5, times = 84), rep(x = 6, times 84), rep(x = 7, times = 84), rep(x = 8, times = 84), rep(x = 9, times 84), rep(x = 10, times = 84), rep(x = 11, times = 84), rep(x = 12, times 84), rep(x = 13, times = 84)) which works but it is super silly and I need to create different variables similar to this, changing the value of the repetition, 84 in this case. Thanks for any help. F. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.