[R] Repeating sequence elements

Rui Barradas ruipbarradas at sapo.pt
Fri May 17 13:01:11 CEST 2013


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.
>



More information about the R-help mailing list