A dataframe in R is a data structure used to store data in a tabular form – in rows and columns. In this tutorial, we will look at how to get the first n rows of an R dataframe with the help of some examples.
How to extract the first n rows in R?
You can use the head()
function in R to extract the first n rows of a dataframe. Pass the dataframe and n (the number of rows you want from the start) as arguments to the head()
function.
The following is the syntax –
# get first n rows head(dataframe, n)
If you do not pass a value for n
, the head()
function returns the first 6 rows of the dataframe by default.
Steps to get the first n rows of a dataframe in R
Let’s now look at a step-by-step example to show the usage of the syntax mentioned above.
Step 1 – Create a dataframe
First, we will create a dataframe that we will be using throughout this tutorial.
# create a dataframe employees_df = data.frame( "Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin", "Meredith", "Creed"), "Age"= c(26, 28, 29, 32, 30, 38, 49), "Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting", "QA", "QA") ) # display the dataframe print(employees_df)
Output:
Name Age Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting 6 Meredith 38 QA 7 Creed 49 QA
We now have a dataframe containing some information about employees in an office. The dataframe has the columns – “Name”, “Age”, and “Department”. Note that the dataframe above has 7 rows.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Step 2 – Extract the first n rows of dataframe using head()
First, let’s use the head()
function without specifying a value for n and see what we get.
# head() with default n print(head(employees_df))
Output:
Name Age Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting 6 Meredith 38 QA
We get the first six rows of the dataframe by default from the head()
function.
Let’s now pass a custom value for n.
For example, let’s just get the first two rows of the above dataframe.
# first two rows print(head(employees_df, 2))
Output:
Name Age Department 1 Jim 26 Sales 2 Dwight 28 Sales
Here, we specify n as 2 and thus we get the first two rows of the dataframe.
Summary – Extract first n rows in R
In this tutorial, we looked at how to get the first n rows of a dataframe in R. The following is a short summary of the steps mentioned in this tutorial.
- Create a dataframe (skip this step if you already have a dataframe to operate on).
- Use the
head()
function to get the first n rows of the dataframe. Pass the number of rows you want from the top as an argument. If you do not specify n, thehead()
function returns the first six rows by default.
You might also be interested in –
- How to Create a DataFrame in R?
- How to Add a Row to a Dataframe in R?
- Rename Column Name in R Dataframe
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.