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")
In order to Filter or subset rows in R we will be using Dplyr package. Dplyr package in R is provided with filter() function which subsets the rows with multiple conditions on different criteria. We will be using mtcars data to depict the example of filtering or subsetting. Filter or subset the rows in R using dplyr.
6.4 dplyr basicsfilter() : pick observations by their values. select() : pick variables by their names. mutate() : create new variables with functions of existing variables. summarise() : collapse many values down to a single summary.
You can use the mutate() function from the dplyr package to add one or more columns to a data frame in R.
Describe what the dplyr package in R is used for. Apply common dplyr functions to manipulate data in R. Employ the 'pipe' operator to link together a sequence of functions. Employ the 'mutate' function to apply other chosen functions to existing columns and create new columns of data.
There are a couple of ways to do this. Assuming you are pipe-lining your data (using %>%)
top_n(tn)
works with grouped data. It will not return tn rows, if the data is sorted with arrange()head(500)
takes the first 500 rows (can be used after arrange(), for example)sample_n(size=500)
can be used to select 500 arbitrary rowsIf you are looking for the R equivalent to SQL's LIMIT, use head().
I think you're actually looking for slice()
here.
filter(pc, condition) %>% slice(1:500)
This does not rank the results. It merely pulls a slice, by position. In this case positions 1 through 500.
If this is coming from a relational db, head
is a better option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With