Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select 20 first rows per each entry

I've got a large dataset (millions of records) with this structure:

id | ident1  |  ident2  
1    A000001    B000001 
2    A000001    B000002
................

99   A000001    B000099
.........
337  A000002    B000037
338  A000002    B000043

In other words, for each [ident1], I have a high number of entries in [ident2]. I'd like to be able to select only 20 of these entries (of all of them, if there's less than 20).

Order is not important: so if a given ident1 has 100 matching [ident2], I'd like either the first 20 entries, or 20 random ones, it doesn't matter.

Thanks in advance, p.

like image 334
user3310782 Avatar asked Jul 22 '26 01:07

user3310782


1 Answers

Try

library(dplyr)
df %>% 
   group_by(ident1) %>%
   slice(1:20) 

Or using data.table

library(data.table)
setDT(df)[, head(.SD,20), by=ident1]

If you need a sample

setDT(df)[df[, .I[sample(.N,20, replace=FALSE)], by=ident1]$V1]

If some of the groups have less than 20 rows to sample

setDT(df)[,if(.N < 20) .SD else .SD[sample(.N,20, replace=FALSE)], by=group]
like image 162
akrun Avatar answered Jul 24 '26 14:07

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!