Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r
function
communitycreator

How to check the dimension of a DataFrame in R

Onyejiaku Theophilus Chidalu

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)
Implementing the dim() 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 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
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring