I want to create a sequence, repeating each element according to a vector. I have this: v <- c(4, 4, 4, 3, 3, 2) And want to create this: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5 6 6 TIA // s R version 3.0.0 (2013-04-03) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base [[alternative HTML version deleted]]
Hello, At an R prompt, type ?rep Then use v <- c(4, 4, 4, 3, 3, 2) rep(1:6, v) Hope this helps, Rui Barradas Em 17-05-2013 11:53, Stefan Petersson escreveu:> I want to create a sequence, repeating each element according to a vector. > > I have this: > > v <- c(4, 4, 4, 3, 3, 2) > > And want to create this: > > 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5 6 6 > > TIA > > // s > > R version 3.0.0 (2013-04-03) > Platform: x86_64-pc-linux-gnu (64-bit) > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=C LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Try rep(1:length(v), v) HTH, Jorge.- On Fri, May 17, 2013 at 8:53 PM, Stefan Petersson <stefan@inizio.se> wrote:> I want to create a sequence, repeating each element according to a vector. > > I have this: > > v <- c(4, 4, 4, 3, 3, 2) > > And want to create this: > > 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5 6 6 > > TIA > > // s > > R version 3.0.0 (2013-04-03) > Platform: x86_64-pc-linux-gnu (64-bit) > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=C LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices utils datasets methods base > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]
Hi, rep(seq_along(v),v) ?#[1] 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5 6 6 A.K. ----- Original Message ----- From: Stefan Petersson <stefan at inizio.se> To: r-help at r-project.org Cc: Sent: Friday, May 17, 2013 6:53 AM Subject: [R] Repeating sequence elements I want to create a sequence, repeating each element according to a vector. I have this: v <- c(4, 4, 4, 3, 3, 2) And want to create this: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5 6 6 TIA ? // s R version 3.0.0 (2013-04-03) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.