Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
r
communitycreator

What is the head() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)
Implementing the head() function

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
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring