Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R rep function with for loop

Tags:

function

r

rep

I have a year range 1981:1984. How can use rep() function to get the following results: For each round rep, the latest year is dropped off: first get 1981:1984, then 1981:1983, then 1981:1982, then 1981 as shown in the table:

1981
1982
1983
1984
1981
1982
1983
1981
1982
1981
like image 403
dennis Avatar asked Feb 04 '23 18:02

dennis


1 Answers

One way:

v = 1981:1984
v[ sequence(rev(seq_along(v))) ]
# [1] 1981 1982 1983 1984 1981 1982 1983 1981 1982 1981
like image 172
Frank Avatar answered Feb 07 '23 07:02

Frank