How to check the dimension of a DataFrame in R
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 frameMy_Data_Frame <- data.frame (Height = c("Tall", "Average", "short"),Body_structure = c("Meso", "Endo", "Ecto"),Age = c(35, 30, 45))# printing the data frameprint(My_Data_Frame)# dimension of the data framedim(My_Data_Frame)
Code explanation
- Line 2: We create a data frame variable,
My_Data_Frame, using thedata.frame()function. The data frame contains three columns. - Line 9: We print the
My_Data_Framedata frame. - Line 12: We check for the dimension of the
My_Data_Framedata frame using thedim()function.
The output of the code above,
3 3, tells us that the data frame we created is a3by3data frame. This means it contains3rows and3columns, respectively.