top_n & top_frac R Functions of dplyr Package (2 Examples)
In this R tutorial you’ll learn how to return the upper part of a data set with the top_n and top_frac functions of the dplyr add-on package.
The article consists of the following:
- Creating Example Data
- Example 1: Select Top N Rows with top_n Function
- Example 2: Select Top Fraction with top_frac Function
- Video, Further Resources & Summary
Let’s dive right into the examples:
Creating Example Data
We’ll use the following data frame as basement for the examples of this R tutorial:
my_data <- data.frame(x1 = 1:5, # Create example data x2 = LETTERS[1:5]) my_data # Print example data # x1 x2 # 1 1 A # 2 2 B # 3 3 C # 4 4 D # 5 5 E |
my_data <- data.frame(x1 = 1:5, # Create example data x2 = LETTERS[1:5]) my_data # Print example data # x1 x2 # 1 1 A # 2 2 B # 3 3 C # 4 4 D # 5 5 E
Furthermore, we need to install and load the dplyr package to R, if we want to use the top_n and top_frac functions:
install.packages("dplyr") # Install dplyr library("dplyr") # Load dplyr |
install.packages("dplyr") # Install dplyr library("dplyr") # Load dplyr
Example 1: Select Top N Rows with top_n Function
The top_n function returns the first N rows of a data frame or tibble. The top_n function can be applied as follows:
top_n(my_data, 3) # Apply top_n function # Selecting by x2 # x1 x2 # 1 3 C # 2 4 D # 3 5 E |
top_n(my_data, 3) # Apply top_n function # Selecting by x2 # x1 x2 # 1 3 C # 2 4 D # 3 5 E
In this example, we returned the first three rows of our data matrix.
Example 2: Select Top Fraction with top_frac Function
We can also extract a percentage of our input data by using the top_frac function:
top_frac(my_data, 0.5) # Apply top_frac function # Selecting by x2 # x1 x2 # 1 4 D # 2 5 E |
top_frac(my_data, 0.5) # Apply top_frac function # Selecting by x2 # x1 x2 # 1 4 D # 2 5 E
In this example, we returned the top 50% of our data frame to the RStudio console.
Video, Further Resources & Summary
I have recently published a video on my YouTube channel, which shows further functions of the dplyr package. You can find the video below:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to read the related articles which I have published on www.statisticsglobe.com.
- sample_n & sample_frac R Functions
- Introduction to the dplyr Package in R
- R Functions List (+ Examples)
- The R Programming Language
In this tutorial you learned how to get the top values of a data frame or tibble with the dplyr package in the R programming language. Let me know in the comments, in case you have additional questions.
Statistics Globe Newsletter