In the below sample data frame, the output vector should be [1] 1,2,6,7,8,8,5,4,3,2,1,9,7,1
P N N+1 N+2 N+3 N+4
2 1 2 3 4 5
3 6 7 8 9 10
2 8 5 7 6 5
5 4 3 2 1 9
2 7 1 1 1 1
Explanation: Values in P dictates how far to the right side should the data frame values be read and appended into the final vector. 2 means, first two values to the right should be read which gives [1] 1,2
If possible, please tell how to do it with and without explicit loop operations. Thanks!