Skip to content
Related Articles

Related Articles

Improve Article
Remove unnecessary values from an Object in R Programming – na.omit() Function
  • Last Updated : 23 Jun, 2020

na.omit() function in R Language is used to omit all unnecessary cases from data frame, matrix or vector.

Syntax: na.omit(data)

Parameter:
data: Set of specified values of data frame, matrix or vector.

Returns: Range of values after NA omission.

Example 1:






# R program to remove 
# unnecessary values
  
# Creating a data frame
data <- data.frame(
      
    # Column with 2 missing values
    x1 = c(1, 2, NA, 5, 9, 7, NA), 
      
    # Column with 1 missing values
    x2 = c(11, 1, NA, 1, 7, 9, 1), 
      
    # Column without missing values
    x3 = c(5, 7, 6, 2, 8, 1, 6)
)  
      
data  
  
# Apply na.omit() function
data_omit <- na.omit(data) 
  
# Print omitted data 
data_omit                                        

Output:

  
  x1 x2 x3
1  1 11  5
2  2  1  7
3 NA NA  6
4  5  1  2
5  9  7  8
6  7  9  1
7 NA  1  6
 
  x1 x2 x3
1  1 11  5
2  2  1  7
4  5  1  2
5  9  7  8
6  7  9  1

Example 2:




# R program to remove 
# unnecessary values
  
# Creating a data frame
data <- data.frame(x1 = c(1, 2, NA, 5, 9, 7, NA)) 
  
# Original data vector with NAs
data$x1     
  
# Calling na.omit() function
na.omit(data$x1) 
  
# Vector without NAs
as.numeric(na.omit(data$x1))       

Output :

[1]  1  2 NA  5  9  7 NA
[1] 1 2 5 9 7
attr(, "na.action")
[1] 3 7
attr(, "class")
[1] "omit"
[1] 1 2 5 9 7



My Personal Notes arrow_drop_up
Recommended Articles
Page :