Number of Rows in R Data Frame

To get number of rows in R Data Frame, call the nrow() function and pass the data frame as argument to this function.

nrow() is a function in R base package.

In this tutorial, we will learn how to use nrow() function to get number of rows in the Data Frame.

Syntax

The syntax to call nrow() function to get the number of rows in given data frame is

</>
Copy
nrow(df)

where

df is the Data Frame for which we would like to find the number of rows.

Return Value

nrow() returns an integer value or NULL value.

Examples

In the following program, we create a data frame, and find the number of rows in it using nrow() function.

Example.R

</>
Copy
#create data frame
df <- data.frame("c1" = c(41, 42, 43, 44),
                 "c2" = c(45, 46, 47, 48),
                 "c3" = c(49, 50, 51, 52))
#find number of rows
n <- nrow(df)
#print
cat("Number of rows in Data Frame :", n)

Output

Number of rows in Data Frame : 4

Now, let us take an empty data frame, and find the number of rows in it. nrow() should return 0, since there are no rows in the data frame.

Example.R

</>
Copy
#create data frame
df <- data.frame()
#find number of rows
n <- nrow(df)
#print
cat("Number of rows in Data Frame :", n)

Output

Number of rows in Data Frame : 0

Conclusion

In this R Tutorial, we learned to get the number of rows in an R Data Frame using nrow() function, with the help of examples.