To count number of rows in R data frame, you can use nrow() function.

The following method shows how you can do it with syntax.

Method: Use nrow() Function

nrow(df)

The following example shows how to use nrow() function to count number of rows in data frame.

Using nrow() Function

Let’s see how to use nrow() function in R:

# Create dafaframe
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H","A","B","A","C","D","B","E","H"),
                 Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58,9.6,8.85,7.89,10.24,12.36,11.45,9.47,8.12),
                 Status=c("OK","Suspect","OK","OK","Suspect","Suspect","Suspect","OK","OK","OK","OK","OK","Suspect","Suspect","Suspect","OK"))

# Count total rows in data frame
r <- nrow(df)

# Print count of rows
print(r)

Output:

[1] 16

The output shows number of rows in data frame which is 16.