4
votes

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
1
why use rep for this task? unlist(sapply(1:4, function(i){1981:(1985-i)})) would do the trick.Jealie
thank both of you. kind of magic.dennis

1 Answers

8
votes

One way:

v = 1981:1984
v[ sequence(rev(seq_along(v))) ]
# [1] 1981 1982 1983 1984 1981 1982 1983 1981 1982 1981