Trusted answers to developer questions

How to check the dimension of a DataFrame in R

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Overview

The dim() function checks for the dimension, i.e, the number of rows and columns present in a data frame.

Syntax

dim(dataframe)

Parameter value

The dim() function takes a single and mandatory parameter value. This value represents the data frame object whose dimension is to be determined.

Return value

The dim() function returns the dimension of a 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)
)
# printing the data frame
print(My_Data_Frame)
# dimension of the data frame
dim(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 print the My_Data_Frame data frame.
  • Line 12: We check for the dimension of the My_Data_Frame data frame using the dim() function.

The output of the code above, 3 3, tells us that the data frame we created is a 3 by 3 data frame. This means it contains 3 rows and 3 columns, respectively.

RELATED TAGS

r
function

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
Did you find this helpful?