3

I am currently trying to subset the first n-observations for each date in my dataset. Let's say n=2 for example purposes. This is what the data set looks like:

Date             Measure  
2019-02-01       5  
2019-02-01       4  
2019-02-01       3  
2019-02-01       6  
   …             …   
2019-02-02       5  
2019-02-02       5  
2019-02-02       2  
   …             …   

I would like to see this output:

Date             Measure  
2019-02-01       5  
2019-02-01       4  
2019-02-02       5  
2019-02-02       5  
   …             … 

Unfortunately, this is not something I am able to do with definitions. I am dealing with over 10 million rows of data, so the solution needs to be dynamic to make the selection of n for each unique date.

M--
  • 20,766
  • 7
  • 52
  • 87
Baili
  • 43
  • 4

1 Answers1

2

An option is to group by 'Date' and slice the sequence of 'n' rows

library(dplyr)
n <- 2
df1 %>%
    group_by(Date) %>%
    slice(seq_len(n))
akrun
  • 789,025
  • 32
  • 460
  • 575