try this:
> x <- read.table(textConnection("group student rating
+ A 1 3
+ A 2 2
+ A 3 2
+ A 4 3
+ A 5 1
+ A 6 2
+ A 7 3
+ B 1 2
+ B 2 3
+ B 3 3
+ B 4 1
+ B 5 1 "), header=TRUE,
as.is=TRUE)>
> # add the columns for the ratings
> ratings <- sort(unique(x$rating)) # get the different ratings
> for (i in ratings){
+ x[[paste('as', i)]] <- ave(x$rating, x$group, FUN=function(a){
+ c(NA, head(cumsum(a == i), -1))
+ })
+ }>
> x
group student rating as 1 as 2 as 3
1 A 1 3 NA NA NA
2 A 2 2 0 0 1
3 A 3 2 0 1 1
4 A 4 3 0 2 1
5 A 5 1 0 2 2
6 A 6 2 1 2 2
7 A 7 3 1 3 2
8 B 1 2 NA NA NA
9 B 2 3 0 1 0
10 B 3 3 0 1 1
11 B 4 1 0 1 2
12 B 5 1 1 1 2
On Sat, Jan 16, 2010 at 11:38 PM, jie feng <jiefeng222@gmail.com> wrote:
> Hi, R experts:
>
> I am asking for helps with manipulating this data: we have different
> groups:
> A,B, C, and so on. In each group, we have different students, for example,
> we have 6 students in A and 5 students in B. Each student gives a rating,
> which ranges from 1 to 3.
>
> We want to manipulate this data and add three more variables called as 1,
> as
> 2 and as 3. For example, in group A, for each student, we count the #
> of student before this student provides rating as 1, rating as 2 and rating
> as 3. For example, for student 2 in group A, there are 0 students before
> him/her provide rating as 1, there are 0 students before him/her provide
> rating as 2, there are 1 students before him/her provide rating as 3. For
> student 3 in group A, there are 0 students before him/her provide rating as
> 1, there are 1 students before him/her provide rating as 2, there are 1
> students before him/her provide rating as 3.
>
> group student rating as 1 as 2 as 3
> A 1 3 -- -- --
> A 2 2 0 0 1
> A 3 2 0 1 1
> A 4 3 0 2 1
> A 5 1 0 2 2
> A 6 2 1 2 2
> A 7 3 1 3 2
> B 1 2 -- -- --
> B 2 3 0 1 0
> B 3 3 0 1 1
> B 4 1 0 1 2
> B 5 1 1 1 2
>
> Best
>
> Jay
>
> [[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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
[[alternative HTML version deleted]]