Replace NA with Zero in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Replace NA with Zero in R appeared first on Data Science Tutorials

Replace NA with Zero in R, Using the dplyr package in R, you can use the following syntax to replace all NA values with zero in a data frame.

Substitute zero for any NA values.

Create new variables from existing variables in R – Data Science Tutorials

df <- df %>% replace(is.na(.), 0)

To replace NA values in a particular column of a data frame, use the following syntax:

In column col1, replace NA values with zero.

df <- df %>% mutate(col1 = ifelse(is.na(col1), 0, col1))

Additionally, you can substitute a NA value in one of a data frame’s several columns using the following syntax.

Test for Normal Distribution in R-Quick Guide – Data Science Tutorials

in columns col1 and col2, replace NA values with zero

df <- df %>% mutate(col1 = ifelse(is.na(col1), 0, col1),
                    col2 = ifelse(is.na(col2), 0, col2))

With the help of the following data frame, the following examples demonstrate how to utilize these functions in practice.

Let’s create a data frame

df <- data.frame(team = c('T1', 'T1', 'T1', 'T2', 'T2', 'T2', 'T2'),
                 position = c('R1', NA, 'R1', 'R1', 'R1', 'R1', 'R2'),
                 points = c(122, 135, 129, NA, 334, 434, 139))

Now we can view the data frame

How to Filter Rows In R? – Data Science Tutorials

df
  team position points
1   T1       R1    122
2   T1     <NA>    135
3   T1       R1    129
4   T2       R1     NA
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 1: Replace every NA value across all columns.

Replace all NA values across all columns of a data frame by running the code below.

library(dplyr)

Yes, now we will replace all NA values with zero

df <- df %>% replace(is.na(.), 0)

Let’s view the data frame

df
  team position points
1   T1       R1    122
2   T1        0    135
3   T1       R1    129
4   T2       R1      0
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 2: In a Specific Column, Replace NA Values

The code below demonstrates how to change NA values in a particular column of a data frame.

One sample proportion test in R-Complete Guide (datasciencetut.com)

library(dplyr)

replace NA values with zero in position column only

df %>% mutate(position = ifelse(is.na(position), 0, position))
team position points
1   T1       R1    122
2   T1        0    135
3   T1       R1    129
4   T2       R1     NA
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 3: Replace any columns with NA values.

The code that follows demonstrates how to change NA values in one of a data frame’s many columns.

library(dplyr)

Now we can replace NA values with zero in position and points columns

What Is the Best Way to Filter by Date in R? – Data Science Tutorials

df %>% mutate(position = ifelse(is.na(position), 0, position),
                    points = ifelse(is.na(points), 0, points))
   team position points
1   T1       R1    122
2   T1        0    135
3   T1       R1    129
4   T2       R1      0
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Using the dplyr package in R, you can use the following syntax to replace all NA values with zero in a data frame.

Substitute zero for any NA values.

df <- df %>% replace(is.na(.), 0)

To replace NA values in a particular column of a data frame, use the following syntax:

In column col1, replace NA values with zero.

df <- df %>% mutate(col1 = ifelse(is.na(col1), 0, col1))

Additionally, you can substitute a NA value in one of a data frame’s several columns using the following syntax.

How to make a rounded corner bar plot in R? – Data Science Tutorials

in columns col1 and col2, replace NA values with zero

df <- df %>% mutate(col1 = ifelse(is.na(col1), 0, col1),
                    col2 = ifelse(is.na(col2), 0, col2))

With the help of the following data frame, the following examples demonstrate how to utilize these functions in practice:

Let’s create a data frame

df <- data.frame(team = c('T1', 'T1', 'T1', 'T2', 'T2', 'T2', 'T2'),
                 position = c('R1', NA, 'R1', 'R1', 'R1', 'R1', 'R2'),
                 points = c(122, 135, 129, NA, 334, 434, 139))

Now we can view the data frame

df
  team position points
1   T1       R1    122
2   T1     <NA>    135
3   T1       R1    129
4   T2       R1     NA
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 1: Replace every NA value across all columns.

Replace all NA values across all columns of a data frame by running the code below.

Best Data Science YouTube Tutorials Free to Learn – Data Science Tutorials

library(dplyr)

Yes, now we will replace all NA values with zero

df <- df %>% replace(is.na(.), 0)

Let’s view the data frame

df
  team position points
1   T1       R1    122
2   T1        0    135
3   T1       R1    129
4   T2       R1      0
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 2: In a Specific Column, Replace NA Values

The code below demonstrates how to change NA values in a particular column of a data frame:

library(dplyr)

replace NA values with zero in position column only

df %>% mutate(position = ifelse(is.na(position), 0, position))
team position points
1   T1       R1    122
2   T1        0    135
3   T1       R1    129
4   T2       R1     NA
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

Example 3: Replace any columns with NA values.

The code that follows demonstrates how to change NA values in one of a data frame’s many columns.

library(dplyr)

Now we can replace NA values with zero in position and points columns

df %>% mutate(position = ifelse(is.na(position), 0, position),
                    points = ifelse(is.na(points), 0, points))
   team position points
1   T1       R1    122
2   T1        0    135
3   T1       R1    129
4   T2       R1      0
5   T2       R1    334
6   T2       R1    434
7   T2       R2    139

The post Replace NA with Zero in R appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)