Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create particular month and/or date sequence "n" number of times

Tags:

date

r

seq

rep

I am trying to create a sequence of months from Jan- Dec 3 times. ie. Jan Feb Mar . . . . Dec Jan Feb Mar . . . . Dec Jan Feb Mar . . . . Dec

I tried this code

rep(c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"),each=3)

but it generates result like this

[1] "Jan"  "Jan"  "Jan"  "Feb"  "Feb"  "Feb"  "Mar"  "Mar"  "Mar"  "Apr"  "Apr"  "Apr" 
[13] "May"  "May"  "May"  "Jun"  "Jun"  "Jun"  "Jul"  "Jul"  "Jul"  "Aug"  "Aug"  "Aug" 
[25] "Sept" "Sept" "Sept" "Oct"  "Oct"  "Oct"  "Nov"  "Nov"  "Nov"  "Dec"  "Dec"  "Dec" 

but I want Jan Feb Mar....Dec Jan Feb Mar.....Dec Jan Feb Mar...Dec

Thanks Harshad

like image 260
Harshad M Avatar asked Dec 19 '22 22:12

Harshad M


1 Answers

We can use month.abb instead of c('Jan', 'Feb'), etc..

 rep(month.abb, 3)

For full month name

 rep(month.name, 3)
like image 92
akrun Avatar answered Dec 21 '22 12:12

akrun