R

# Length() Function in R

There is a requirement to count the total number of elements in the R data structures like vector, list, and dataframe. In this scenario, R supports the built-in functions that return a total number of elements. The length() function is used to return the count of all elements in the R data structure.

In this article, we will discuss about length() and the usage of length() on the vector, list, and dataframe,

## Get the Length of a Vector

As we know, vector stores the elements in a linear format. The length() function returns the count of elements present in the given vector. It takes the vector object as a parameter.

Syntax:

length(vector_object)

Parameter:
vector_object is a vector

Example 1:
In this example, we create a vector with 10 numeric elements and return the length of a vector.

#create vector with 10 mango prices
mango=c(34,54,32,56,78,65,89,90,90,8)

#return length of the mango
print(length(mango))

Result:

We can see that the total elements present in the vector is 10.

Example 2:
In this example, we create a vector with 5 string elements and return the length of a vector.

#create vector with 5 mango types
mango=c("type1","type2","type3","type4","type5")

#return length of the mango
print(length(mango))

Result:

We can see that the total elements present in the vector is 5.

## Get the Length of a List

As we know, the list stores the elements in a linear format. The length() function returns the count of elements present in the given list. It takes the list object as a parameter.

Syntax:

length(list_object)

Parameter:
list_object is a list.

Example 1:
In this example, we create a list with 10 numeric elements and return the length of a list.

#create list with 10 mango prices
mango=list(34,54,32,56,78,65,89,90,90,8)

#return length of the mango
print(length(mango))

Result:

We can see that the total elements present in the list is 10.

Example 2:
In this example, we create a list with 5 string elements and return the length of a list.

#create list with 5 mango types
mango=list("type1","type2","type3","type4","type5")

#return length of the mango
print(length(mango))

Result:

We can see that the total elements present in the list is 5.

Example 3:
In this example, we create a nested list with 6 string elements and return the length of a list.

#create nested list with 6 mango types
mango=list("type1","type2","type3","type4","type5",list("type6","type7","type8"))

#return length of the mango
print(length(mango))

Result:

We can see that the total elements present in the list is 6.

## Get the Length of a Dataframe

As we know, the dataframe stores the elements in a row and column format. If we directly apply the length function on a dataframe, it returns the number of columns. To get the number of rows in column, you have to specify the column name.

Syntax:

Total number of columns – length(dataframe_object)

Total number of rows in a column– length(dataframe_object\$column)

Where:

dataframe_object is the dataframe

column is the column name

Example 1:

In this example, we return the total number of columns from the dataframe using the length() function.

#create a dataframe-market that has 4 rows and 5 columns.
market=data.frame(market_id=c(1,2,3,4), market_name=c('M1','M2','M3','M4'), market_place=c('India','USA','India','Australia'), market_type=c('grocery','bar','grocery','restaurent'), market_squarefeet=c(120,342,220,110))

#return length of dataframe
print(length(market))

Result:

The total number of columns in the previous dataframe is 5, so it returns 5.

Example 2:

In this example, we return the total number of rows in the market_id and market_name columns from the dataframe using the length() function.

#create a dataframe-market that has 4 rows and 5 columns.
market=data.frame(market_id=c(1,2,3,4), market_name=c('M1','M2','M3','M4'), market_place=c('India','USA','India','Australia'), market_type=c('grocery','bar','grocery','restaurent'), market_squarefeet=c(120,342,220,110))

#return length of the market_id column
print(length(market\$market_id))

#return length of the market_name column
print(length(market\$market_name))

Result:

The total number of rows in both columns are 4, so it returns 4.

## Conclusion

In this article, we have seen how to apply the length() function on a vector, dataframe, and list. It returns the total number of elements present in the given data structure.