Removing Missing values in R-Quick Guide

Removing Missing values in R, To return values in R that are not NA values, use the following syntax.

Removing Missing values in R

Only values that aren’t NA are returned.

x <- x[!is.na(x)]

The examples below demonstrate how to utilize this syntax in R with both vectors and data frames.

Line types in R: Ultimate Guide For R Baseplot and ggplot » finnstats

Approach 1: Return Values in a Vector That Aren’t NA

The code below shows how to return non-NA values from a vector.

make a vector first,

x <- c(139, 254, 456,NA, 46, NA, 79,NA)

To return only values that are not NA

x <- x[!is.na(x)]
x
[1] 139 254 456  46  79

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

The code below explains how to get the rows in a data frame that don’t have a NA value in a certain column.

Feature Scaling in Machine Learning-Introduction » finnstats

create a data frame

df <- data.frame(team=c('Q1', 'Q2', 'Q3', 'Q4', 'Q5'),
                 kick=c(13, NA, 31, 32, 14),
                 pass=c(32, NA, NA, 34, 35))
df
  team kick pass
1   Q1   13   32
2   Q2   NA   NA
3   Q3   31   NA
4   Q4   32   34
5   Q5   14   35

Rows containing NA in the ‘pass’ column should be removed.

df <- df[!(is.na(df$pass)), ]
df

Now we can view the data frame

team kick pass
1   Q1   13   32
4   Q4   32   34
5   Q5   14   35

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

The code below explains how to return the rows in a data frame that do not have a NA value in one of the multiple columns.

Introduction to Deep Learning » finnstats

set up a data frame first,

df <- data.frame(team=c('Q1', 'Q2', 'Q3', 'Q4', 'Q5'),
                 kick=c(13, NA, 31, 32, 14),
                 pass=c(32, NA, NA, 34, 35))
df

view the data frame

   team kick pass
1   Q1   13   32
2   Q2   NA   NA
3   Q3   31   NA
4   Q4   32   34
5   Q5   14   35

Let’s remove rows with NA in the kick and pass column

df <- df[!(is.na(df$kick)) & !(is.na(df$pass)), ]

Yes, view the data frame

df
  team kick pass
1   Q1   13   32
4   Q4   32   34
5   Q5   14   35

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

The following code returns the rows in a data frame that have no NA value in any column.

Transition plot in R-change in time visualization » finnstats

Start with data frame

df <- data.frame(team=c('Q1', NA, 'Q3', 'Q4', 'Q5'),
                 kick=c(13, NA, 31, 32, 14),
                 pass=c(32, NA, NA, 34, 35))
df
  team kick pass
1   Q1   13   32
2 <NA>   NA   NA
3   Q3   31   NA
4   Q4   32   34
5   Q5   14   35

Now remove rows with NA in any column

df <- na.omit(df)
df
  team kick pass
1   Q1   13   32
4   Q4   32   34
5   Q5   14   35

Have you found this article to be interesting? I’d be glad if you could forward it to a friend or share it on Twitter or Linked In to help it spread.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

7 − two =

finnstats