Built with Tutify.io

    AIStudyLabs

    Replace Zero (0) with NA on R Dataframe Column

    Replace Zero (0) with NA on R Dataframe Column

    How to replace zero with NA on the data frame columns of single/multiple? You can use R base df[] notation to replace zero with NA value. NA is used in R to indicate missing values, which is different from 0 or empty strings("). In this article, I will explain how to replace all occurrences of zeros with NAs on single/multiple columns of a data frame using the following approaches of R.

    Create a data frame with numeric columns which has zero(0) values.

    
    # Create dataframe with numeric columns
    df = data.frame(pages=c(32,0,0),
                            chapters=c(20,86,0),
                            price=c(144,0,321))
    print("Create data frame:")
    print(df)
    

    Yields below output.

    <a id="h-replace-0s-with-nas-in-an-r"></a>

    Replace 0s with NAs in an R

    To replace 0 with NA in a dataframe using the first R approach, which is the df[] notation, you can pass the condition (df == 0) into the df[] notation. This will check if the column value in the dataframe is 0, and if it is, it will be replaced with the NA value.

    Related: You cal also replace NA with 0 in R .

    
    # Replace zeros with NA on data frame 
    df[df == 0] <- NA
    print("After replacing zeros with NA:")
    print(df)
    

    Yields below output.

    <a id="h-replace-0s-with-nas-in-selected-columns"></a>

    Replace 0s with NAs in Selected Columns

    Alternatively, you can use df[] notation to replace the zeros(0) with NA values on selected columns of the data frame. For this, you need to check if each value of the specified column is equal to 0. If it is 0, you can replace it with NA.

    
    #Example 3 - Replace on selected columns
    df["pages"][df["pages"] == 0] <- NA
    print(df)
    
    # Output
    #  pages chapters price
    # 1    32       20   144
    # 2    NA       86     0
    # 3    NA        0   321
    

    <a id="h-replace-using-negation"></a>

    Replace using Negation

    You can also achieve this using the negation operator and is.na() function.

    
    # Replace using negation
    is.na(df) <- !df
    print("After replacing zeros with NA:")
    print(df)
    

    Yields the same output as above

    <a id="h-using-the-r-replace-function-to-replace-0s-with-nas"></a>

    Using the R replace() function to Replace 0s with NAs

    To replace all occurrences of zero values with NA values in a data frame, you can use the R replace() function. Simply pass the data frame along with the condition (representing the values to be replaced) and NA (representing the new values to replace with) into the function. This will give you a new data frame where all the zeros have been replaced with NAs.

    
    #Example 4 - Using replace() function
    df <- replace(df, df==0, NA)
    print(df)
    
    # Output
    #  pages chapters price
    # 1    32       20   144
    # 2    NA       86    NA
    # 3    NA       NA   321
    

    <a id="h-replace-0s-with-nas-using-r-dplyr"></a>

    Replace 0s with NAs using R dplyr

    So far, we have implemented the replacing process using R base functions, Now we will see how to implement the replacing using the third-party package of R which is the dplyr package.

    You can use the na_if() method to replace all zeros with NAs in an entire data frame. Before going to use the functions of the dplyr package we need to install it as install.packages('dplyr') and load it using library("dplyr").

    
    # Replace using dplyr::na_if()
    library("dplyr")  
    df <- na_if(df, 0)
    print(df)
    
    # Output
    #   pages chapters price
    # 1    32       20   144
    # 2    NA       86    NA
    # 3    NA       NA   321
    

    <a id="h-replace-0s-with-nas-using-dplyr-mutate-all"></a>

    Replace 0s with NAs using dplyr::mutate_all()

    To replace all presence of zeros in an entire data frame with NA you can use another function of the dplyr package is mutate_all() function. Let's apply it to a data frame and get the updated data frame where all zeros are substituted with NAs.

    
    # Replace using dplyr::mutate_all() 
    library(dplyr) 
    df <- df %>% mutate_all(~na_if(., 0))
    print(df)
    
    # Output
    #  pages chapters price
    # 1    32       20   144
    # 2    NA       86    NA
    # 3    NA       NA   321
    

    <a id="h-replace-on-all-numeric-columns"></a>

    Replace on All Numeric columns

    The mutate_if() function of the dplyr package is used to replace all occurrences of zeros only on numeric columns of the data frame with NA values. Since we have all numeric columns, it updates all columns of zero values with NA values.

    
    # Replace only on all Numeric columns
    library(dplyr) 
    df <- df %>% mutate_if(is.numeric, ~na_if(., 0))
    print(df)
    
    # Output
    #  pages chapters price
    # 1    32       20   144
    # 2    NA       86    NA
    # 3    NA       NA   321
    

    <a id="h-replace-zero-with-na-only-on-selected-columns"></a>

    Replace Zero with NA Only on Selected Columns

    mutate_at() from dplyr is used to target specific columns (pages) in the dataframe df and replace 0 with NA.

    
    # Replace only on selected columns
    library(dplyr) 
    df <- df %>% mutate_at(c('pages'), ~na_if(., 0))
    print(df)
    
    # Output
    #  pages chapters price
    # 1    32       20   144
    # 2    NA       86     0
    # 3    NA        0   321
    

    <a id="h-replace-zero-with-na-on-the-selected-column-indexs"></a>

    Replace Zero with NA on the Selected Column Indexs

    mutate_at() function from the dplyr package to replace all zeros with NAs on specified index position columns in the R dataframe.

    
    # Replace only on selected column index
    library(dplyr) 
    df <- df %>% mutate_at(c(2), ~na_if(., 0))
    print(df) 
    
    # Output
    #  pages chapters price
    # 1    32       20   144
    # 2     0       86     0
    # 3     0       NA   321
    

    <a id="h-replacing-on-tibble"></a>

    Replacing on tibble

    If you have tibble, use the following approach to replace it. Use tibble() to create a tibble.

    
    # Replacing on tibble
    df <-tibble(
      col1 = c("A", B, "NA"),
      col2 = c(0, 2, NA),
      col3 = c(1, NA, 5)
    )
    df <- df %>% mutate_if(is.numeric , replace_na, replace = 0)
    print(df)
    
    # Output
    #  A tibble: 3 × 3
    #  col1   col2  col3
    #   <chr> <dbl> <dbl>
    # 1 A         0     1
    # 2 B         2     0
    # 3 NA        0     5
    

    <a id="h-frequently-asked-questions-of-replace-zeros-with-nas-in-r"></a>

    Frequently Asked Questions of Replace zeros with NAs in R

    You can replace zeros with NAs using base R functions like ifelse() or by using the dplyr package to make the operation more efficient.

    You can specify the columns where you want to replace zeros with NAs by subsetting the data frame or using functions like mutate_at() or across() from the dplyr package.

    You can apply a function across all columns using lapply() or across() to replace zeros with NAs in the entire data frame.

    While there are no packages solely for this purpose, dplyr, data.table, and base R functions provide efficient methods for replacing zeros with NAs.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to replace all occurrences of zeros with NAs on single/multiple columns of a data frame using multiple approaches of R such as df[] notation, replace(), and dplyr package functions like na_if(), mutate_all(), mutate_if(), and mutate_at().

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References