0

I saw similar posts like this and that but they were not keeping all rows. I have a dataset looks like this:

id <- c(1,1,1,1, 2,2,2)
item.id <- c(1,3,4,4, 2,1,1)
sequence <- c(1,1,1,2, 1,1,2)

data <- data.frame("id"=id, "item.id"=item.id, "sequence"=sequence)
> data
  id item.id sequence
1  1       1        1
2  1       3        1
3  1       4        1
4  1       4        2
5  2       2        1
6  2       1        1
7  2       1        2

id represents for each student, item.id represents the questions students take, sequence is the attempt number for each item.id.

For each id, I would like to count the number of unique item.ids per id. More importantly, I would like to add this item.count variable to the data frame. Below is the desired output, keeping all rows and adding the count information for each id:

> data
  id item.id sequence  item.count
1  1       1        1    3
2  1       3        1    3
3  1       4        1    3
4  1       4        2    3
5  2       2        1    2
6  2       1        1    2
7  2       1        2    2

Any insights? Thanks

amisos55
  • 1,913
  • 1
  • 10
  • 21

0 Answers0