What is the head() function in R? (2024)

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Overview

The head() function in R is used to determine the first or last part of an R object.

Syntax

head(x, n)

Parameter value

The head() function takes the following parameter values.

  • x: This represents the R object (a matrix, data.frame, ftable, table, function).
  • n: This represents a single integer. When given a zero or positive value, the output object contains the number of elements for a given vector, the rows for a given matrix or data frame, or lines for a given function. When given a negative value, all but the first or last n number of elements of the input R object are returned.

Return value

The head() function returns an object like that of the input object, x.

Example

# creating a data frame object

mydataframe<- data.frame(

numbers = 1:10,

alphabets = LETTERS[1:10],

symbol = c('*', '!', '#', '?', ',', '/', '=', '+', '<', ')')

)

# implementing the head() function returning only 7 rows of the data frame

head(mydataframe, n=7)

Explanation

  • Lines 2 to 6: We create an R object (a data frame) mydataframe using the data.frame() function.
  • Line 9: We implement the head() function by making the resulting data frame have only 7 rows.

RELATED TAGS

function

r

CONTRIBUTOR

What is the head() function in R? (1)Onyejiaku Theophilus Chidalu

What is the head() function in R? (2024)

FAQs

What is the head() function in R? ›

The head() function in R is used to display the first n rows present in the input data frame. In this section, we are going to get the first n rows using head() function. For this process, we are going to import a dataset 'iris' which is available in R studio by default.

What does head() do in R? ›

Returns the first or last parts of a vector, matrix, table, data frame or function.

What does the head () function do? ›

Definition and Usage

The head() method returns a specified number of rows, string from the top. The head() method returns the first 5 rows if a number is not specified. Note: The column names will also be returned, in addition to the specified rows.

What is the head and tail function in DataFrame in R? ›

What is the head() and tail() functions do in R? The head() in R is used to get the first rows of the DataFrame, Vector, or compatible object. The tail() is used to get the last rows of the DataFrame, Vector, or a compatible object.

What is the difference between head and str in R? ›

Use head() to view the first few rows. Use tail() to view the first few rows. Use str() to look at the structure of the data.

What does a head of function do? ›

Head of Function means a person who exercises major managerial responsibilities, is responsible for a significant business or operating unit, or has senior managerial responsibility for maintaining accounts or other records of the licensee.

Why head command is used? ›

The head command allows you to see the initial lines of a file in standard output without opening a file.

What is the basic function of head? ›

Function. The head contains sensory organs: two eyes, two ears, a nose and tongue inside of the mouth. It also houses the brain. Together, these organs function as a processing center for the body by relaying sensory information to the brain.

What does a head do? ›

A head is the part of an organism which usually includes the ears, brain, forehead, cheeks, chin, eyes, nose, and mouth, each of which aid in various sensory functions such as sight, hearing, smell, and taste. Some very simple animals may not have a head, but many bilaterally symmetric forms do, regardless of size.

What are heads functions? ›

The head anatomy contains structures that control and protect our ability to see, hear, speak, and think. The head encompasses bony and soft tissue structures at the apex of the spine. A body cannot function without the brain located inside the head. The bony skull protects the brain and its neurovascular structures.

What is tail() in R? ›

tail: Return the First or Last Part of an Object

Returns the first or last parts of a vector, matrix, table, data frame or function. Since head() and tail() are generic functions, they may also have been extended to other classes.

How to see the first 10 rows in R? ›

Alternative Method: Using head() Function in R

In addition to top_n(), R provides the head() function for selecting the first 'n' rows of a DataFrame. Here, we demonstrate how to use head() to achieve the same result. The output of the above code will also display the top 10 rows of the dataframe df.

What is head vs tail list? ›

From the definition of a list, any element, when prepended to a list, is also a list. For example, 1:[2,3] is also a list. Here, 1 is called the head of the list, and 2 is called the tail of the list.

What does head () in R mean? ›

The head() function in R is used to display the first n rows present in the input data frame. In this section, we are going to get the first n rows using head() function. For this process, we are going to import a dataset 'iris' which is available in R studio by default.

How to use head function? ›

The head() function is primarily used to view the first few rows of a dataset. It helps users quickly get an overview of the data and its structure. Analysts can check column names, data types, and the data itself by displaying the initial records.

What does str() do in R? ›

str() function in R Language is used for compactly displaying the internal structure of a R object. It can display even the internal structure of large lists which are nested. It provides one liner output for the basic R objects letting the user know about the object and its constituents.

What is the function in the head? ›

Function. The head contains sensory organs: two eyes, two ears, a nose and tongue inside of the mouth. It also houses the brain. Together, these organs function as a processing center for the body by relaying sensory information to the brain.

How to use headers in R? ›

You can create RStudio headers using the Ctrl + Shift + R command. A text box will automatically pop up for you to name. This ensures that the length of all headers are equal and it saves you time from typing out the dashes. You may choose to use other heading styles (that won't be tracked by RStudio).

What does head do in machine learning? ›

A head of machine learning is a senior-level position that oversees the design, development, and deployment of machine learning solutions for various business problems.

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 5736

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.