Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205372

Sequence of Repeated Values in R

$
0
0

This is a very basic question, but it's annoying me, so I'm asking.

I need a sequence of repeated numbers, i.e. 1 1 ... 1 2 2 ... 2 3 3 ... 3 etc. The way I implemented this was

  nyear <- 20
  names <- c(rep(1,nyear),rep(2,nyear),rep(3,nyear),rep(4,nyear),
             rep(5,nyear),rep(6,nyear),rep(7,nyear),rep(8,nyear))

which works, but is clumsy, and obviously doesn't scale well.

How do I repeat the N integers M times each in sequence? I tried nesting seq() and rep() but that didn't quite do what I wanted. I can obviously write a for-loop to do this, but there should be an intrinsic way to do this!


Viewing all articles
Browse latest Browse all 205372

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>