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]]
Rafael Moral
2009-Dec-20 12:16 UTC
[R] Res: how to create three new variables? Thanks a lot!
Hello! Here is a way:> mydata <- data.frame(third_before=c(6,10), second_before=c(9,10), first_before=c(10,8)) > mydatathird_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 <jiefeng222@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]]
David Winsemius
2009-Dec-20 14:10 UTC
[R] how to create three new variables? Thanks a lot!
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
Possibly Parallel Threads
- how can I delete those columes with the same element in every row?
- {nlme} Multilevel estimation heteroscedasticity
- How to monthly,daily,yearly average
- another question: how to delete one of columes in two ones with high correlation(0.95)
- 2nd DC, internal DNS: dns_tkey_negotiategss: TKEY is unacceptable