Hello!

Here is a way:

> mydata <- data.frame(third_before=c(6,10), second_before=c(9,10), 
> first_before=c(10,8))
> mydata
  third_before second_before first_before
1            6             9           10
2           10            10            8

And if you already have a dataset and wants only to create three more columns, 
create the 3 variables independently and use cbind:

> third_before=c(6,10)
> second_before=c(9,10)
> first_before=c(10,8)
> cbind(your.dataset, third_before, second_before, first_before)

Hope this helps,
All the best!

Rafael



________________________________
De: jie feng <jiefeng...@gmail.com>
Para: R-help@r-project.org
Enviadas: Domingo, 20 de Dezembro de 2009 6:34:20
[[elided Yahoo spam]]

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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



      
____________________________________________________________________________________
[[elided Yahoo spam]]

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to