Table of Contents
Listwise deletion is a method of handling missing data in a dataset by removing entire rows that contain missing values. This technique is commonly used in data analysis to ensure accurate results and avoid bias. In R, listwise deletion can be performed using the na.omit() function, which removes all rows with missing values from the dataset. An example of listwise deletion in R would be as follows:
dataset
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 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 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
Additional Resources
The following tutorials explain how to perform other common tasks in R:
Cite this article
stats writer (2024). How do I perform listwise deletion in R? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-i-perform-listwise-deletion-in-r-can-you-provide-an-example/
stats writer. "How do I perform listwise deletion in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-i-perform-listwise-deletion-in-r-can-you-provide-an-example/.
stats writer. "How do I perform listwise deletion in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-i-perform-listwise-deletion-in-r-can-you-provide-an-example/.
stats writer (2024) 'How do I perform listwise deletion in R? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-i-perform-listwise-deletion-in-r-can-you-provide-an-example/.
[1] stats writer, "How do I perform listwise deletion in R? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do I perform listwise deletion in R? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
