Do not worry if you want to get the first element in a vector, matrix, or data frame in R. In this article, we introduce to you a function that responds to your desire. It is the first()
function. Keep reading. We will show you the syntax and provide examples to use the function.
The first()
function
The first()
function applies to an object and returns the first element if the object is a vector, matrix, list, etc, and returns a column if the object is a data frame.
Syntax:
first(object)
Parameter:
- object: An iterable object such as a vector, list, data frame, etc.
Some examples of the first() function
Use the first()
function to get the first element of a vector
Vector is the most popular data type in R. In this example, we will show you how to get the first element from a vector.
Code:
# Import required library library("dplyr") # Declare a vector v <- c(4, 5, 1, 0, 12, 14, 21, 8, 9) cat("The first element of the vector is:", first(v))
Result:
The first element of the vector is: 4
Use the first()
function to get the first element of a matrix
We can create a matrix from one or many vectors. Therefore, we will use the vector in the example above to create a matrix, then, get the first element of the matrix and see the result.
Code:
library("dplyr") # Declare a vector v <- c(4, 5, 1, 0, 12, 14, 21, 8, 9) # Create a matrix from the vector m <- matrix(v, nrow = 3, ncol = 3) cat("The first element of the matrix is:", first(m))
Result:
The first element of the matrix is: 4
Use the first()
function to get the first element of a list
As long as the object is iterable, we can apply the first()
function to get the first element. In this part, we will apply the function to a list to pick up the first element.
Code:
library("dplyr") # Declare a list l <- list("hello", 2, 3.14, NA, 22, TRUE) cat("The first element of the list is:", first(l))
Result:
The first element of the list is: hello
Use the first()
function to get the first row of a data frame
A data frame is created from one or many vectors and each vector is a column. As a result, when using the first()
function in a data frame, we will get a column.
Code:
library("dplyr") # Declare vectors v1 <- c(3, 1, 5, 22, 9, 0) v2 <- c(12, 5, 2, 0, 0, 16) v3 <- c(10, 21, 12, 4, 7, 6) # Create a data frame from vectors df <- data.frame(v1, v2, v3) cat("The first column of the data frame is:", first(df))
Result:
The first column of the data frame is: 3 1 5 22 9 0
Summary
In summary, the first()
function returns the first element of the input object. If the object is a dataframe, the result will be a column. Although the function is simple, it is useful when working with large data and brings significant results.
Maybe you are interested:
- ordered in R: How to create or convert the ordered factor in R
- The ln() function in R
- lead function in R: Set the NA values at the end of the R object.
My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP