Skip to content
Related Articles

Related Articles

Improve Article

Return a Logical Vector with Missing Values removed in R Programming – complete.cases() Function

  • Last Updated : 19 Jun, 2020

complete.cases() function in R Language is used to return a logical vector with cases which are complete, i.e., no missing value.

Syntax: complete.cases(x)

Parameters:
x: Object

Example 1:




# R Program to return
# cases which are complete
  
# Creating a vector
vec <- c(1, 2, 3, 4, NA, 3)
  
# Calling complete.cases() function
complete.cases(vec)
  
# Printing the returned vector
vec1 <- vec[complete.cases(vec)]
vec1

Output:

[1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
[1] 1 2 3 4 3

Example 2:




# R Program to return
# cases which are complete
  
# Calling pre-defined data set
x <- airquality[1:15, ]
x
  
# Calling complete.cases() Function
complete.cases(x)
  
# Printing data set
x1 <- x[complete.cases(x[, 1]), ]
x1

Output:

   Ozone Solar.R Wind Temp Month Day
1     41     190  7.4   67     5   1
2     36     118  8.0   72     5   2
3     12     149 12.6   74     5   3
4     18     313 11.5   62     5   4
5     NA      NA 14.3   56     5   5
6     28      NA 14.9   66     5   6
7     23     299  8.6   65     5   7
8     19      99 13.8   59     5   8
9      8      19 20.1   61     5   9
10    NA     194  8.6   69     5  10
11     7      NA  6.9   74     5  11
12    16     256  9.7   69     5  12
13    11     290  9.2   66     5  13
14    14     274 10.9   68     5  14
15    18      65 13.2   58     5  15
 [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
[13]  TRUE  TRUE  TRUE
   Ozone Solar.R Wind Temp Month Day
1     41     190  7.4   67     5   1
2     36     118  8.0   72     5   2
3     12     149 12.6   74     5   3
4     18     313 11.5   62     5   4
6     28      NA 14.9   66     5   6
7     23     299  8.6   65     5   7
8     19      99 13.8   59     5   8
9      8      19 20.1   61     5   9
11     7      NA  6.9   74     5  11
12    16     256  9.7   69     5  12
13    11     290  9.2   66     5  13
14    14     274 10.9   68     5  14
15    18      65 13.2   58     5  15



My Personal Notes arrow_drop_up
Recommended Articles
Page :