Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
function
communitycreator

How to obtain the length of a data frame in R

Onyejiaku Theophilus Chidalu

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)
Syntax

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)
Implementing the length() function

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.

RELATED TAGS

r
function
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring