9 C
London
Monday, March 10, 2025
HomeStatistics TutorialRHow to Use “Is Not NA” in R

How to Use “Is Not NA” in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to return values in R that are not NA values:

#return only values that are not NA
x !is.na(x)]

The following examples show how to use this syntax with both vectors and data frames in R.

Example 1: Return Values that are Not NA in Vector

The following code shows how to return the values in a vector that are not NA:

#create vector
x 

#return only values that are not NA
x !is.na(x)]

[1]  1 24  6  9

Example 2: Return Rows that are Not NA in One Column of Data Frame

The following code shows how to return the rows in a data frame that do not have an NA value in a specific column:

#create data frame
df frame(x=c(1, 24, NA, 6, NA, 9),
                 y=c(NA, 3, 4, 8, NA, 12),
                 z=c(NA, 7, 5, 15, 7, 14))

#view data frame
df

   x  y  z
1  1 NA NA
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

#remove rows with NA in z column
df !(is.na(df$z)), ]

#view data frame
df

   x  y  z
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

Example 3: Return Rows that are Not NA in Several Columns

The following code shows how to return the rows in a data frame that do not have an NA value in one of several specific columns:

#create data frame
df frame(x=c(1, 24, NA, 6, NA, 9),
                 y=c(NA, 3, 4, 8, NA, 12),
                 z=c(NA, 7, 5, 15, 7, 14))

#view data frame
df

   x  y  z
1  1 NA NA
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

#remove rows with NA in x or y column
df !(is.na(df$x)) & !(is.na(df$y)), ]

#view data frame
df

   x  y  z
2 24  3  7
4  6  8 15
6  9 12 14

Example 4: Return Rows that are Not NA in Any Column

The following code shows how to return the rows in a data frame that do not have an NA value in any column:

#create data frame
df frame(x=c(1, 24, NA, 6, NA, 9),
                 y=c(NA, 3, 4, 8, NA, 12),
                 z=c(NA, 7, 5, 15, 7, 14))

#view data frame
df

   x  y  z
1  1 NA NA
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

#remove rows with NA in any column
df omit(df)

#view data frame
df

   x  y  z
2 24  3  7
4  6  8 15
6  9 12 14

Additional Resources

How to Replace NAs with Strings in R
How to Impute Missing Values in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories