Complete Cases in R (Example)
The following R code shows how to apply the complete.cases() function in the R programming language.
Example Data Frame
my_data <- data.frame(x = c(3, 6, NA, 3, 2), # Create example data y = letters[1:5], z = c(NA, 2, 1, 1, 2)) my_data # Print example data # x y z # 3 a NA # 6 b 2 # NA c 1 # 3 d 1 # 2 e 2 |
my_data <- data.frame(x = c(3, 6, NA, 3, 2), # Create example data y = letters[1:5], z = c(NA, 2, 1, 1, 2)) my_data # Print example data # x y z # 3 a NA # 6 b 2 # NA c 1 # 3 d 1 # 2 e 2
Example 1: Basic Application of complete.cases Function in R
The complete.cases function returns a logical vector, indicating whether a row is complete (i.e. without NA values).
complete.cases(my_data) # Apply complete.cases function # FALSE TRUE FALSE TRUE TRUE |
complete.cases(my_data) # Apply complete.cases function # FALSE TRUE FALSE TRUE TRUE
Example 2: Listwise Deletion in R
The complete.cases function can be used to perform listwise deletion:
data_complete <- my_data[complete.cases(my_data), ] # Listwise deletion in R data_complete # Print reduced data # x y z # 6 b 2 # 3 d 1 # 2 e 2 |
data_complete <- my_data[complete.cases(my_data), ] # Listwise deletion in R data_complete # Print reduced data # x y z # 6 b 2 # 3 d 1 # 2 e 2
Example Video
The following video shows further examples for the application of complete.cases in R: