The “head” Function in R
Package: Base R (no specific package required)
Purpose: Returns the first n elements of a vector, matrix, or data frame.
General Class: Utility/Basic
Required Argument(s):
x: A vector, matrix, or data frame.
Notable Optional Arguments:
n: The number of elements to display. The default is 6.
nrow: The number of rows to display for a matrix or data frame. The default is n or 6 if n is not specified.
ncol: The number of columns to display for a matrix or data frame. The default is 1 for a vector or ncol for a data frame.
Example:
# Example vector
data <- c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
# Display the first 5 elements of the vector
result <- head(data, n = 5)
print(result)In this example, the head function is used to display the first 5 elements of the numeric vector data. The result is then printed to the console.