I have a large dataframe in R in which users were tasked to describe objects in a scene. I needed unique 3 users per scene, however some scenes got described more than 3 times. I'm attempting to keep the first 3 unique users and remove the rest.
Toy data (the real dataset has many more rows and columns)
user <- c("A", "A", "A", "B", "B", "C", "C", "D", "E", "E", "F", "F", "F")
scene <- c("library", "library", "library", "park", "park", "library", "library", "park", "library", "library", "library", "library", "library")
object <- c("book", "book", "lamp", "dog", "cat", "book", "lamp", "dog", "desk", "desk", "book", "lamp", "lamp")
index <- c(1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2)
dat <- data.frame(user, scene, object, index)
user scene object index
A library book 1
A library book 2
A library lamp 1
B park dog 1
B park cat 1
C library book 1
C library lamp 1
D park dog 1
E library desk 1
E library desk 2
F library book 1
F library lamp 1
F library lamp 2
... ... ... ...
For example, here A
, B
, and C
were the first users to describe the scene library
. So now F
's description is not needed. My main problem is that while I can get overall count of unique users, I don't know how to label them as 1
, 2
, 3
, etc in order to chop off the values past 3.
Desired output
user scene object index count
A library book 1 1
A library book 2 1
A library lamp 1 1
B park dog 1 1
B park cat 1 1
C library book 1 2
C library lamp 1 2
D park dog 1 2
E library desk 1 3
E library desk 2 3
This was helpful but only groups by one column, so I've not been able to apply it here: R - Group by variable and then assign a unique ID