Skip to content

Data Science Tutorials

For Data Science Learners

  • best books about data analytics
    Best Books About Data Analytics Course
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
  • Best GGPlot Themes
    Best GGPlot Themes You Should Know R
  • Positive or Negative in R R
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 R
  • pheatmap function in R
    The pheatmap function in R R
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
Replace NA with Zero in R

Replace NA with Zero in R

Posted on June 29June 26 By Admin No Comments on Replace NA with Zero in R

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

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Find the Maximum Value by Group in R
Next Post: How to Find Unmatched Records in R

Related Posts

  • KPSS test in R
    KPSS Test in R With Examples R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • How to Recode Values in R
    How to Recode Values in R R

Leave a Reply Cancel reply

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

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Gamma distribution in R
    Gamma distribution in R R
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • Beginner's Guide to Data Science
    Beginner’s Guide to Data Science Machine Learning
  • Divide data into groups in R R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • Correlation By Group in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme