Home » How to Replace NA with Zero in dplyr

How to Replace NA with Zero in dplyr

by Erma Khan
spot_img

You can use the following syntax to replace all NA values with zero in a data frame using the dplyr package in R:

#replace all NA values with zero
df % replace(is.na(.), 0)

You can use the following syntax to replace NA values in a specific column of a data frame:

#replace NA values with zero in column named col1
df % mutate(col1 = ifelse(is.na(col1), 0, col1))

And you can use the following syntax to replace NA value in one of several columns of a data frame:

#replace NA values with zero in columns col1 and col2
df % mutate(col1 = ifelse(is.na(col1), 0, col1),
                    col2 = ifelse(is.na(col2), 0, col2))

The following examples show how to use these function in practice with the following data frame:

#create data frame
df frame(player=c('A', 'B', 'C', 'D', 'E'),
                 pts=c(17, 12, NA, 9, 25),
                 rebs=c(3, 3, NA, NA, 8),
                 blocks=c(1, 1, 2, 4, NA))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C  NA   NA      2
4      D   9   NA      4
5      E  25    8     NA

Example 1: Replace All NA Values in All Columns

The following code shows how to replace all NA values in all columns of a data frame:

library(dplyr)

#replace all NA values with zero
df % replace(is.na(.), 0)

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   0    0      2
4      D   9    0      4
5      E  25    8      0

Example 2: Replace NA Values in a Specific Column

The following code shows how to replace NA values in a specific column of a data frame:

library(dplyr)

#replace NA values with zero in rebs column only
df % mutate(rebs = ifelse(is.na(rebs), 0, rebs))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C  NA    0      2
4      D   9    0      4
5      E  25    8     NA

Example 3: Replace NA Values in One of Several Columns

The following code shows how to replace NA values in one of several columns of a data frame:

library(dplyr)

#replace NA values with zero in rebs and pts columns
df % mutate(rebs = ifelse(is.na(rebs), 0, rebs),
                    pts = ifelse(is.na(pts), 0, pts))

#view data frame
df

  player pts rebs blocks
1      A  17    3      1
2      B  12    3      1
3      C   0    0      2
4      D   9    0      4
5      E  25    8     NA

Additional Resources

How to Filter Rows that Contain a Certain String Using dplyr
How to Calculate Relative Frequencies Using dplyr
How to Select the First Row by Group Using dplyr

spot_img

Related Posts