Reputation: 179
I currently have a dataset like:
ID RESULTS
1 M
1 A
1 M
1 C
1 B
2 Q
2 E
2 S
2 G
2 Z
......
From this, I would like to keep the first 3 rows, by group. Meaning, I'd like:
ID RESULTS
1 M
1 A
1 M
2 Q
2 E
2 S
I dug around in data.table, the closest I found was using something like mult
or .I
. Does anyone have a simple workaround? Thanks!
Upvotes: 1
Views: 71
Reputation: 2539
I would suggest a more concise way. You can have more detail with ?data.table
or with example(data.table)
DT = data.table(ID=rep(c(1,2),each=5),RESULTS=
c("M","A","M","C","B","Q","E","S","G","Z"))
> DT[,.SD[1:3],by=ID]
## ID RESULTS
## 1: 1 M
## 2: 1 A
## 3: 1 M
## 4: 2 Q
## 5: 2 E
## 6: 2 S
Upvotes: 2