How To Count Rows in R Data Frames using nrow

Utilize the data.frame(table()) Action to Count Number of Rows in R

The data.frame(table()) action creates a table with the tally of distinct factor amounts. It counts the total exclusive rows of a file. We can simply pass the necessary file of the DataFrame to the action. View the following code snippet with imputed data science.

df : data.frame(Name = c(“Jack”,”Jay”,”Mark”,”Sam”),
Month = c(“Jan”,”Jan”,”May”,”July”),
Age = c(12,10,15,13))
data.frame(table(df$Month))

Output:

Var1 Freq
1 Jan 2
2 July 1
3 May 1

Utilize the count() Function to Tally the Number of Rows in R

The plyr library in R executes basic data handling tasks like splitting data, performing some action, and merging it later. It possesses a function count() which yields the frequency of exclusive rows of a DataFrame. Having to pass the DataFrame and file name as its parameter, displayed below:

df : data.frame(Name = c(“Jack”,”Jay”,”Mark”,”Sam”),
Month = c(“Jan”,”Jan”,”May”,”July”),
Age = c(12,10,15,13))
library(plyr)
count(df, “Month”)

Output:

Month freq
1 Jan 2
2 July 1
3 May 1

Utilize the ddply() Function to Count Amount of Rows in R

Another interesting action provided in the plyr library is the ddply() action. It breaks the data into a subset, identifies some action to be attached to the data, and incorporate the result. In the example beneath, we shall pass the file name and DataFrame to the action, not using ncol, but nrow action as parameters:

df : data.frame(Name = c(“Jack”,”Jay”,”Mark”,”Sam”),
Month = c(“Jan”,”Jan”,”May”,”July”),
Age = c(12,10,15,13))
library(plyr)
ddply(df, .(Month), nrow)

Output:

Month V1
1 Jan 2
2 July 1
3 May 1

You can utilize the nrow() action to tally the total of rows in a data grid in R:

#tally total rows in data grid
nrow(df)

#tally total rows with no NA values in any file of data grid
nrow(na.omit(df))

#tally total rows with no NA values in specific file of data grid
nrow(df[!is.na(df$column_name),])

The next examples display how to utilize the nrow() action in practice.

Example 1: Tally Total Number of Duplicate Rows

The next code displays how to tally the total number of rows in a data grid:

#create data grid
df : data.frame(var1=c(1, 3, 3, 4, 5),
var2=c(7, 7, 8, 6, 2),
var3=c(9, 9, 6, 6, 8),
var4=c(1, 1, 2, 8, 9))

#view data grid
df

var1 var2 var3 var4
1 1 7 9 1
2 3 7 9 1
3 3 8 6 2
4 4 6 6 8
5 5 2 8 9

#tally total rows in data grid
nrow(df)

[1] 5

There are 5 summed rows in this data grid.

Example 2: Tally Rows with No NA Values in Any Row

The following code displays how to tally the sum number of rows in a data grid with no NA values in every column:

#create data grid
df : data.frame(var1=c(1, 3, 3, 4, 5),
var2=c(7, 7, 8, NA, 2),
var3=c(9, 9, NA, 6, 8),
var4=c(1, 1, 2, 8, 9))

#view data grid
df

var1 var2 var3 var4
1 1 7 9 1
2 3 7 9 1
3 3 8 NA 2
4 4 NA 6 8
5 5 2 8 9

#tally total rows in data grid with no NA values in any column of data grid
n row(na.omit(df))

[1] 3

There are 3 sum rows in this data grid that have no NA values in every column.

Example 3: Count Rows containing No NA Values in Certain Column

The next code displays how to sum number of row count in a data frame containing no NA values in every column:

#create data frame
df : data.frame(var1=c(1, 3, 3, 4, 5),
var2=c(7, 7, 8, NA, 2),
var3=c(9, NA, NA, 6, 8),
var4=c(1, 1, 2, 8, 9))

#view data frame
df

var1 var2 var3 var4
1 1 7 9 1
2 3 7 NA 1
3 3 8 NA 2
4 4 NA 6 8
5 5 2 8 9

#count summed rows in data frame containing no NA values in ‘var2’ column of data grid
nrow(df[!is.na(df$var2),])

[1] 4
There are 4 total distinct rows in this data frame that have no NA values in the ‘var2’ column.

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

nrow(df)
where

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

Return Value

nrow() yields integer values or NULL values.

Scroll to top
Privacy Policy