Open In App

How to find length of data frame in R

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see What is a Data Frame and how to find the length of a data frame in R programming Language.

Return the length (total number of rows) of the Data Frame using nrow()

nrow() function is used to return the number of rows of the specified object (Matrix/DataFrame etc). we will use this function which will return the total number of rows present in the given dataframe. In this way, we can get the length of the Data Frame.

Syntax:

nrow(dataframe)

Where data is the input data frame.

Let’s create a data frame with 4 rows and 3 columns and return a total number of rows.

R
# Create three vectors
Id = c(1,2,3,4)
Name = c("Sai","Sita","Ram","Ramya")
Subject = c("R", "Python", "Java","Web technologies")
 
# Create DataFrame from the above vectors
dataframe = data.frame(Id, Name, Subject)
print(dataframe)

# Get the total number of rows
cat("Number of rows: ", nrow(dataframe))

Output:

  Id  Name          Subject
1 1 Sai R
2 2 Sita Python
3 3 Ram Java
4 4 Ramya Web technologies

Number of rows: 4

Let’s create an empty dataframe and return the length of the dataframe.

R
# Create an empty dataframe
dataframe = data.frame(vector())

# Get the total number of rows
cat("Length of the DataFrame: ", nrow(dataframe))

Output:

Length of the DataFrame:  0

Return length (total number of columns) of Data Frame using ncol()

ncol() function is used to return the number of columns of the specified object (Matrix/DataFrame etc). we will use this function which will return the total number of columns present in the given dataframe. By this way, we can get the length of the Data Frame.

Syntax:

ncol(dataframe)

Where data is the input dataframe.

Let’s create a dataframe with 4 rows and 3 columns and return total number of columns.

R
# Create three vectors
Id = c(1,2,3,4)
Name = c("Sai","Sita","Ram","Ramya")
Subject = c("R", "Python", "Java","Web technologies")
 
# Create DataFrame from the above vectors
dataframe = data.frame(Id, Name, Subject)
print(dataframe)

# Get the total number of columns
cat("Number of columns: ", ncol(dataframe))

Output:

  Id  Name          Subject
1 1 Sai R
2 2 Sita Python
3 3 Ram Java
4 4 Ramya Web technologies

Number of columns: 3

Return length (total number of rows & columns) of Data Frame using dim()

dim() function is used to get the dimension of the specified dataframe. It will return total number rows and columns of the dataframe.

Syntax:

dim(dataframe)

Where data is the input dataframe.

Let’s create a dataframe with 4 rows and 3 columns and return the total number of rows and columns of the dataframe.

R
# Create three vectors
Id = c(1,2,3,4)
Name = c("Sai","Sita","Ram","Ramya")
Subject = c("R", "Python", "Java","Web technologies")

# Create DataFrame from the above vectors
dataframe = data.frame(Id, Name, Subject)
print(dataframe)

# Get the total number of rows and columns
dim(dataframe)

Output:

  Id  Name          Subject
1 1 Sai R
2 2 Sita Python
3 3 Ram Java
4 4 Ramya Web technologies

[1] 4 3

Here 4,3 show that we have 4 rows and 3 columns so dim function give total number of rows and columns of a dataframe together.

Conclusion

In conclusion, determining the length of a dataframe in R is a straightforward process that can be achieved using the nrow() function and ncol() function. These functions returns the number of rows and columns in the dataframe, effectively providing its length. Alternatively, the dim() function can also be used to retrieve the dimensions of the dataframe, with the number of rows being the first element of the returned vector. Both methods offer simple and efficient ways to ascertain the size of a dataframe in R, enabling users to gain insights into the structure and extent of their data.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads