userJT userJT - 9 days ago 5
R Question

Using dplyr::filter, how can the output be limited to just first 500 rows?

dplyr is a great and fast library.

Using the %.% operator enables powerful manipulation.

In my first step, I need to limit the output to only 500 rows max (for display purposes).

How can I do that?

par<-filter(pc,Child_Concept_GID==as.character(mcode)) %>% select(Parent_Concept_GID)

what I need is something like


Is there direct way or a nice workaround without making the first step a separate step (outside the %>% "stream")


To limit the output of filter, one can add after filter a function


credit goes to commenter joran


par<-filter(pc,Child_Concept_GID==as.character(mcode)) %>% top_n(500) %>% select(Parent_Concept_GID)