How to use the length() function to get the length of a list in R
Overview
The length() function in R gets the length of a list object.
In simpler terms, it finds the number of items in a list.
Syntax
length(x)
Parameter value
The length() function takes a list object as its single parameter value.
Return value
The length() function returns the list object’s length passed to it.
Code example
In the code below, we use the length() function to determine a list’s length:
# creating a listmylist <- list(1, 2, 3, 4, 5)# using the length functionlength(mylist)
Code explanation
We use the length() function in the code above to determine the number of items in the list variable my_variable. This returns 5.