Dear R experts:

I have following data structure: student id, exam time and score. I want to
create three new columes: 1 st before, 2nd before and 3rd before.
For example, for student 1's 4th exam score is assumed to be influenced by
his previous three cloest  scores, which are 6, 9, and 10 in that order.
For student 2's 5th exam score is assumed to be inflenced by her previous
cloest three scores, which are 10, 10 and 8 in that order.
Note that for student 1, his first time score "10" can not be influenced by
other scores, thus has three empties in the three new columes.
for student 1, his second score "9" is only influenced by his first score
thus has two empties in the columes of 2nd before and 3rd before.
Now I only have 3 columes: id, time and score, and I want to create three
new columes: 1st before, 2nd before and 3rd before. How can I do that?
Thanks a lot.

Best

Jay



               id          time        score         1st before     2nd
before   3rd before
     1 1 10   1 2 9 10  1 3 6 9 10  1 4 10 6 9 10 1 5 5 10 6 9 2 1 7   2 2 8
7  2 3 10 8 7  2 4 10 10 8 7 2 5 5 10 10 8 2 6 9 5 10 10

	[[alternative HTML version deleted]]


