Remove NA Values from Vector in R - GeeksforGeeks (2024)

Improve

In this article, we are going to discuss how to remove NA values from the vector.

Method 1: Using is.na()

We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. !is.na() will get the values except na.

Syntax:

vector[!is.na(vector)]

where the vector is the input vector

Example: R program to remove NA values using above method

Output:

[1] 1 2 NA 4 5 NA 4 5 6 NA

[1] 1 2 4 5 4 5 6

Method 2: Using na.rm

we can also remove na values by computing the sum, mean, variance.

Syntax:

sum(vector, na.rm = TRUE)

where

  • na.rm is used to remove na
  • if na.rm=TRUE it will not consider na
  • if na.rm=FALSE it will consider na

Syntax:

mean(vector, na.rm = TRUE)

Syntax:

var(vector, na.rm = TRUE)

Example: R program to remove na by using sum, var, and mean

R

# create a vector

a=c(1,2,NA,4,5,NA,4,5,6,NA)

# display a

a

# remove NA by computing variance

var(a, na.rm = TRUE)

# remove NA by computing sum

sum(a, na.rm = TRUE)

# remove NA by computing mean

mean(a, na.rm = TRUE)

Output:

[1] 1 2 NA 4 5 NA 4 5 6 NA[1] 3.142857[1] 27[1] 3.857143

Method 3 : Using omit() method

omit() method is used to remove the NA values directly by resulting in the non-NA values and omitted NA values indexes.

Syntax:

na.omit(vector)

where the vector is the input vector

Return type:

  • Returns the non-NA values
  • Returns the indexes of NA values which are removed from the vector

Note: Indexing starts with 1

Example: R program to consider a vector and remove NA values

R

# create a vector with integers along with NA

a=c(1,2,NA,4,5,NA,4,5,6,NA)

# display

print(a)

print("_______________________")

# remove NA using omit() function

a=na.omit(a)

# display vector

print(a)

Output:

[1] 1 2 NA 4 5 NA 4 5 6 NA[1] "_______________________"[1] 1 2 4 5 4 5 6attr(,"na.action")[1] 3 6 10attr(,"class")[1] "omit"


Last Updated : 31 Aug, 2021

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Remove NA Values from Vector in R - GeeksforGeeks (2024)

FAQs

Remove NA Values from Vector in R - GeeksforGeeks? ›

omit() function in R. The na. omit() function in R Programming Language is used to remove missing values (NAs) from a data frame, matrix, or vector.

How to remove na values in vector in R? ›

How can NA values be removed in Excel and R? By using na. omit() , complete. cases() , rowSums() , and drop_na() methods you can remove rows that contain NA ( missing values) from R data frame.

How do you exclude NA values in R? ›

In base R, use na. omit() to remove all observations with missing data on ANY variable in the dataset, or use subset() to filter out cases that are missing on a subset of variables.

How to remove values from a vector in R? ›

In R, you can remove specific elements from a vector by using the subset() function. This function allows you to subset a vector based on certain criteria and remove the elements that match the criteria. You can also use the subset() function to remove multiple elements in one go.

How to remove NA values from mean in R? ›

To drop the missing values from the calculation use na. rm = TRUE. which means remove the NA values.

How do you exclude Na from a vector? ›

You can call max(vector, na. rm = TRUE) . More generally, you can use the na. omit() function.

How do you remove values from a vector? ›

The vector::clear() method removes all elements from the vector, reducing its size to 0. The vector::erase() method is used to remove specific elements or a range of elements from the vector.

How to remove NA values in a column in R? ›

Remove rows with NA of one column in R DataFrame Using drop_na() drop_na() Drops rows having values equal to NA. To use this approach we need to use “tidyr” library, which can be installed.

How to remove NA values in R using Dplyr? ›

Using na.

omit() function removes all the rows which has any NA value in a given data frame.

How to select NA values in R? ›

To select rows with NA values in R, we can use logical indexing combined with the is.na function. The is.na function returns a logical vector indicating which elements are NA. In this code snippet, we use the apply function to apply the any and is.na functions row-wise.

How do I reduce a vector in R? ›

The reduce function performs an operation on a list of vectors one at a time and returns the result as a single value. Syntax: **Reduce (function, seq)** where, Function: user defined or built in function Seq: the input vector/list This recipe demonstrates an example of reduce () function in R.

How do you remove Na values from data? ›

Here we are using na. omit() function to remove rows that contain any NA values. This function checks each row and removes any row that contains one or more NA values. It returns a subset of the original data frame without the rows that have missing values.

How to remove values from data in R? ›

How to Remove Rows in R?
  1. Using Subsetting. One of the most straightforward ways to remove rows in R is by subsetting the data. ...
  2. Using the subset() Function. ...
  3. Removing Duplicates. ...
  4. Using the dplyr Package. ...
  5. Removing Rows by Row Index. ...
  6. Deleting Rows by Range. ...
  7. Deleting Rows by Name.
Sep 19, 2023

How to replace values with NA in R? ›

Replacing values with NA
  1. tidyr::replace_na() : Missing values turns into a value (NA –> -99)
  2. naniar::replace_with_na() : Value becomes a missing value (-99 –> NA)
Mar 5, 2024

How to remove NA values from a column in R? ›

Remove rows with NA of one column in R DataFrame Using drop_na() drop_na() Drops rows having values equal to NA. To use this approach we need to use “tidyr” library, which can be installed.

How to replace NA with 0 in vector r? ›

Method 1: Using the is.na() Function

The ` is.na(data) ` function used in this example returns a logical vector containing TRUE for NAs and FALSE for non-missing values inside the vector. This logical vector is then used to index the data vector, and all the NAs are replaced with 0s.

Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6463

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.