na.omit Function in R (Example)
The R code on this page shows how to omit rows containing NA values with the na.omit() function in the R programming language.
Example Data
original_data <- data.frame(x1 = c(NA, 1:3), # Create example data x2 = c(letters[1:3], NA), x3 = "X") original_data # Print example data # x1 x2 x3 # NA a X # 1 b X # 2 c X # 3 <NA> X |
original_data <- data.frame(x1 = c(NA, 1:3), # Create example data x2 = c(letters[1:3], NA), x3 = "X") original_data # Print example data # x1 x2 x3 # NA a X # 1 b X # 2 c X # 3 <NA> X
Example: Apply na.omit Function to Remove Rows with NAs
omitted_data <- na.omit(original_data) # Apply listwise deletion omitted_data # Print updated data # x1 x2 x3 # 1 b X # 2 c X |
omitted_data <- na.omit(original_data) # Apply listwise deletion omitted_data # Print updated data # x1 x2 x3 # 1 b X # 2 c X
After applying the na.omit function, all rows with at least one NA value are removed. This approach is also called listwise deletion.
Handling NA Values in R
The following video shows further examples and functions for the handling of missing data in the R programming language: