dplyr : select n first rows of each group

13,977 views
Skip to first unread message

Julien Barnier

unread,
Mar 3, 2014, 10:18:09 AM3/3/14
to manip...@googlegroups.com
Hi,

Sorry, this is certainly quite a basic question, but what is the "best"
way (in terms of efficiency) to select the n first rows of each group
with dplyr ?

Is there a special function, like n(), that would return the current row
number in the group and that could be used in a condition with
filter() ? Or am I just missing the obvious ?

Thanks a lot,
-- Julien Barnier Centre Max Weber ENS de Lyon

Victor Kryukov

unread,
Mar 6, 2014, 10:26:36 PM3/6/14
to manip...@googlegroups.com
Hi Julien,

You will need window functions, described here: http://cran.at.r-project.org/web/packages/dplyr/vignettes/window-functions.html, and then function you need is called row_number.


> x <- tbl_df(data.frame(a = rep(1:500), b = rep(letters[1:5], 100))) %.% arrange(b)

> x

Source: local data frame [500 x 2]


    a b

1   1 a

2   6 a

3  11 a

4  16 a

5  21 a

6  26 a

7  31 a

8  36 a

9  41 a

10 46 a

.. .. .


> x %.% group_by(b) %.% filter(row_number(a) <= 10)

Source: local data frame [50 x 2]

Groups: b


    a b

1   1 a

2   6 a

3  11 a

4  16 a

5  21 a

6  26 a

7  31 a

8  36 a

9  41 a

10 46 a

11  2 b

12  7 b

13 12 b

14 17 b

15 22 b

16 27 b

17 32 b

18 37 b

19 42 b

20 47 b

21  3 c

Hadley Wickham

unread,
Mar 10, 2014, 12:49:41 PM3/10/14
to Victor Kryukov, manipulatr
In a future version, you'll be able to use a bare row_number():

x %.% group_by(b) %.% filter(row_number() <= 10)

(that will be quite a bit faster)

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at http://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages