[R] how to create three new variables? Thanks a lot!

David Winsemius dwinsemius at comcast.net
Sun Dec 20 15:10:16 CET 2009


On Dec 20, 2009, at 4:34 AM, jie feng wrote:

> 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.

The combination of your choice of text editors and your use of HTML  
mail format (contrary to the Posting Guide ... please read it more  
carefully)  has completely mangled your posting of the data. As a  
result it is not possible for ordinary humans to figure out what your  
starting point really is. It's possible that the answer lies in the  
help page for lag:

?lag

Future postings on this matter ought to include the data in parse-able  
R format (also described in the Posting Guide.)

>
> 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
>

>  http://www.R-project.org/posting-guide.html

--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list