Replace NA Values with Zeros in DataFrame in R

Here are 2 ways to replace NA values with zeros in a DataFrame in R:

(1) Replace NA values with zeros across the entire DataFrame:

df[is.na(df)] <- 0

Note that if your DataFrame contains factors, you may consider adding “,stringsAsFactors = FALSE” at the end of your DataFrame (later you’ll see an example that tackles this scenario).

(2) Replace NA values with zeros under a single DataFrame column:

df["Column Name"][is.na(df["Column Name"])] <- 0

In the following section, you’ll see how to apply the above syntax in practice.

Steps to Replace NA Values with Zeros in a DataFrame in R

Step 1: Create a DataFrame in R with NA values

Let’s start by creating a DataFrame with 4 columns. Additionally, let’s add several NA values across the entire DataFrame:

df <- data.frame(Group_A =  c(11,11,NA,NA,22,33,33,NA),
                 Group_B =  c(99,99,NA,77,77,NA,55,55),
                 Group_C =  c("Green","Green",NA,"Blue","Blue",NA,"Red","Red"),
                 Group_D =  c(NA,NA,"Blue","Yellow","Yellow","Purple",NA,NA)
                 )

print(df)

As you can see in red, there are currently 11 NA values across the DataFrame:

Replace NA Values with Zeros in a DataFrame in R

Step 2: Replace the NA values with zeros

You can use the following syntax to replace the NA values with zeros across the entire DataFrame:

df[is.na(df)] <- 0

Here is the full code for our example:

df <- data.frame(Group_A =  c(11,11,NA,NA,22,33,33,NA),
                 Group_B =  c(99,99,NA,77,77,NA,55,55),
                 Group_C =  c("Green","Green",NA,"Blue","Blue",NA,"Red","Red"),
                 Group_D =  c(NA,NA,"Blue","Yellow","Yellow","Purple",NA,NA)
                 )

df[is.na(df)] <- 0

print(df)

Notice that not all the NA values were replaced with zeros (only the ones under the columns that contained numeric data were replaced). For the last two columns, where the data type is factor, the NA values may not be replaced:

Example of data

To deal with factors, you can then add “,stringsAsFactors = FALSE” at the end of your DataFrame, as captured below:

df <- data.frame(Group_A =  c(11,11,NA,NA,22,33,33,NA),
                 Group_B =  c(99,99,NA,77,77,NA,55,55),
                 Group_C =  c("Green","Green",NA,"Blue","Blue",NA,"Red","Red"),
                 Group_D =  c(NA,NA,"Blue","Yellow","Yellow","Purple",NA,NA)
                 ,stringsAsFactors = FALSE
                 )

df[is.na(df)] <- 0

print(df)

As you can see, all the NA values are now replaced with zeros across the entire DataFrame:

Replace NA Values with Zeros in a DataFrame in R

Step 3 (optional): Replace NA values under a single DataFrame column

Optionally, you can use the following syntax to replace the NA values with zeros under a single DataFrame column:

df["Column Name"][is.na(df["Column Name"])] <- 0

For example, let’s replace the NA values under the ‘Group_D‘ column:

df <- data.frame(Group_A =  c(11,11,NA,NA,22,33,33,NA),
                 Group_B =  c(99,99,NA,77,77,NA,55,55),
                 Group_C =  c("Green","Green",NA,"Blue","Blue",NA,"Red","Red"),
                 Group_D =  c(NA,NA,"Blue","Yellow","Yellow","Purple",NA,NA)
                 ,stringsAsFactors = FALSE
                 )

df["Group_D"][is.na(df["Group_D"])] <- 0

print(df)

As you can see, only the NA values under the ‘Group_D’ column were replaced:

Single column