Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • ggpairs in R
    ggpairs in R R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • display the last value of each line in ggplot
    How to add labels at the end of each line in ggplot2? R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • Artificial Intelligence Examples
    Artificial Intelligence Examples-Quick View Course
  • Count Observations by Group in R
    Count Observations by Group in R R
Replace NA with Zero in R

Replace NA with Zero in R

Posted on June 29June 26 By Jim No Comments on Replace NA with Zero in R
Tweet
Share
Share
Pin

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.

Tweet
Share
Share
Pin
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

  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • Remove Rows from the data frame in R
    Remove Rows from the data frame in R R
  • Difference between R and Python
    Difference between R and Python R
  • Change ggplot2 Theme Color in R
    Change ggplot2 Theme Color in R ggthemr Package R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • Top 7 Skills Required to Become a Data Scientist
    Top 7 Skills Required to Become a Data Scientist Machine Learning
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Best Books on Data Science with Python
    Best Books on Data Science with Python Course
  • Best Online Course For Statistics
    Free Best Online Course For Statistics Course
  • How to Get a Job as a Data Engineer
    How to Get a Job as a Data Engineer? R
  • Filtering for Unique Values
    Filtering for Unique Values in R- Using the dplyr R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme