userJT userJT - 1 month ago 7
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

filter(pc,CONDITION,rows=500)


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

Answer

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

top_n()

credit goes to commenter joran

solution

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