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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


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 Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.