Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:55:35+00:00 2026-06-05T18:55:35+00:00

I have a dataframe with some numeric columns. Some row has a 0 value

  • 0

I have a dataframe with some numeric columns. Some row has a 0 value which should be considered as null in statistical analysis. What is the fastest way to replace all the 0 value to NULL in R?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:55:36+00:00Added an answer on June 5, 2026 at 6:55 pm

    Replacing all zeroes to NA:

    df[df == 0] <- NA
    

    Explanation

    1. It is not NULL what you should want to replace zeroes with. As it says in ?'NULL',

    NULL represents the null object in R

    which is unique and, I guess, can be seen as the most uninformative and empty object.1 Then it becomes not so surprising that

    data.frame(x = c(1, NULL, 2))
    #   x
    # 1 1
    # 2 2
    

    That is, R does not reserve any space for this null object.2 Meanwhile, looking at ?'NA' we see that

    NA is a logical constant of length 1 which contains a missing value
    indicator. NA can be coerced to any other vector type except raw.

    Importantly, NA is of length 1 so that R reserves some space for it. E.g.,

    data.frame(x = c(1, NA, 2))
    #    x
    # 1  1
    # 2 NA
    # 3  2
    

    Also, the data frame structure requires all the columns to have the same number of elements so that there can be no “holes” (i.e., NULL values).

    Now you could replace zeroes by NULL in a data frame in the sense of completely removing all the rows containing at least one zero. When using, e.g., var, cov, or cor, that is actually equivalent to first replacing zeroes with NA and setting the value of use as "complete.obs". Typically, however, this is unsatisfactory as it leads to extra information loss.

    2. Instead of running some sort of loop, in the solution I use df == 0 vectorization. df == 0 returns (try it) a matrix of the same size as df, with the entries TRUE and FALSE. Further, we are also allowed to pass this matrix to the subsetting [...] (see ?'['). Lastly, while the result of df[df == 0] is perfectly intuitive, it may seem strange that df[df == 0] <- NA gives the desired effect. The assignment operator <- is indeed not always so smart and does not work in this way with some other objects, but it does so with data frames; see ?'<-'.

    1 The empty set in the set theory feels somehow related.
    2 Another similarity with the set theory: the empty set is a subset of every set, but we do not reserve any space for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two columns: an ID and some numeric value
I have a data frame with several columns; some numeric and some character. How
I am using R for some statistical analysis of time series. I have tried
I have a data frame where some of the columns contain NA values. How
I have a data frame in R that has come about from running some
I have a data frame with an id column and some (potentially many) columns
I have a data frame with a grouping variable 'ID' and some values ('Value'):
I have a DataFrame with a few columns. One columns contains a symbol for
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a big dataframe with columns such as: ID, time, OS, IP Each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.