Learn R Programming

FastUtils (version 0.2.1)

getfirst: Get the First Elements of a Vector or List

Description

This function retrieves the first n elements of a vector or list.

Usage

getfirst(x, n = 1)

# S3 method for default getfirst(x, n = 1)

Value

The first n elements of the input.

Arguments

x

A vector, list, or other supported data type.

n

An integer specifying the number of elements to retrieve from the start. Default is 1.

Examples

Run this code
# Get the first element of a vector
getfirst(c(1, 2, 3, 4, 5))
# Get the first 2 elements of a vector
getfirst(c(1, 2, 3, 4, 5), 2)
# Get the first element of a list
getfirst(list("a", "b", "c"))
# Get the first 2 elements of a list
getfirst(list("a", "b", "c"), 2)

Run the code above in your browser using DataLab