How to Perform Listwise Deletion in R (With Example)


Listwise deletion is a method that deletes all rows from a data frame that have a missing value in any column.

The easiest way to perform listwise deletion in R is to use the following syntax:

complete_df <- df[complete.cases(df), ]

This syntax uses the complete.cases() function to create a new data frame that only contains the rows from an original data frame that have no missing values in any column.

The following example shows how to use this function in practice.

Example: Perform Listwise Deletion in R

Suppose we have the following data frame in R that contains information about various basketball players:

#create data frame
df <- data.frame(rating=c(70, 75, 75, 78, 81, 85, 89, 91, 94, 97),
                 points=c(12, 15, 14, 13, NA, 29, 24, 18, 20, 25),
                 assists=c(9, 5, NA, 5, 7, 8, 11, 12, 13, 11))

#view data frame
df

   rating points assists
1      70     12       9
2      75     15       5
3      75     14      NA
4      78     13       5
5      81     NA       7
6      85     29       8
7      89     24      11
8      91     18      12
9      94     20      13
10     97     25      11

Notice that two rows contain NA values in certain columns.

We can use the following syntax to perform listwise deletion and only keep the rows that have no missing values in any column:

#create new data frame that only contains rows with no missing values
complete_df <- df[complete.cases(df), ]

#view new data frame
complete_df

   rating points assists
1      70     12       9
2      75     15       5
4      78     13       5
6      85     29       8
7      89     24      11
8      91     18      12
9      94     20      13
10     97     25      11

Notice that none of the rows in this new data frame have empty values in any column.

Also note that we could use the nrow() function to find how many rows in the original data frame had missing values in any column:

#count how many rows have missing values in any column
nrow(df[!complete.cases(df), ])

[1] 2

This tells us that 2 rows in the original data frame had missing values in at least one column.

And we can just as easily count how many rows did not have missing values in any column:

#count how many rows do not have missing values in any column
nrow(df[complete.cases(df), ])

[1] 8

This tells us that 8 rows in the original data frame did not have missing values in any column.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Find and Count Missing Values in R
How to Interpolate Missing Values in R

Leave a Reply

Your email address will not be published. Required fields are marked *