# How to Count Rows in R

To count the number of rows in R, you can use the nrow()” function. The “nrow()” function accepts the data frame as an argument and returns an integer value suggesting the number of rows in the data frame.

### Syntax

``nrow(data)``

### Parameters

The data can be a matrix, vector, array, or data frame.

### Example

Let’s create a data frame with four rows and three columns and count the number of rows using the nrow() function.

``````df <- data.frame(
students = c("Michael", "Justin", "Taylor", "Selena"),
marks = c(90, 80, 85, 75),
levels = c(10, 7, 8, 7)
)

nrow(df)``````

Output

``[1] 4``

As you can see, our data frame has four rows and returns four.

## Counting rows with No NA values in any column in R

To count the number of rows in a data frame that has no missing values (for example. No NA values) in any of the columns, use the na.omit() function in combination with the nrow() function.

The na.omit() function will return a logical vector with a value of TRUE for each row with no missing values and a value of FALSE for each row with one or more missing values.

Use this logical vector with the nrow() function to count the number of TRUE values, giving you the number of rows with no missing values.

``````df <- data.frame(
students = c("Michael", "Justin", "Taylor", "Selena"),
marks = c(90, 80, NA, 75),
levels = c(10, 7, 8, NA)
)

print(df)

cat("----Counting no na value rows----", "\n")

nrow(na.omit(df))``````

Output

``````   students  marks  levels
1  Michael    90     10
2  Justin     80      7
3  Taylor     NA      8
4  Selena     75     NA

----Counting no na value rows----

[1] 2``````

You can see rows 3rd and 4th filled with NA values, and rows 1st and 2nd have no NA values. So that’s why it returns two.

The nrow() function counts the number of TRUE values in the vector, which would give the number of rows with no missing values. Finally, it would print the result to the console.