How to obtain the length of a data frame in R

Overview

In this shot, we look at how to obtain the length of a data frame in R.

The length of a data frame is the number of columns present in the data frame.

Obtaining the length of a data frame

The length() function obtains the length of a data frame.

Syntax

length(dataframe)

Parameter value

The length() function takes a single and mandatory parameter that represents the data frame object whose length we have to determine.

Return value

The length() function returns the length of a given data frame.

Code example

# creating a data frame
My_Data_Frame <- data.frame (
Height = c("Tall", "Average", "short"),
Body_structure = c("Meso", "Endo", "Ecto"),
Age = c(35, 30, 45)
)
# determine the length of the data frame
length(My_Data_Frame)

Code explanation

  • Line 2: We create a data frame variable, My_Data_Frame, using the data.frame() function. The data frame contains three columns.

  • Line 9: We use the length() function to obtain the length of My_Data_Frame.

Free Resources