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

  • Home
  • SEARCH
  • 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 8778605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:34:46+00:00 2026-06-13T19:34:46+00:00

Possible Duplicate: Set NA to 0 in R I have a data.frame with a

  • 0

Possible Duplicate:
Set NA to 0 in R

I have a data.frame with a column having NA values. I want to replace NA with 0 or any other value. I have tried a lot of threads and methods but it did not give me the result. I have tried the below methods.

a$x[a$x == NA] <- 0;
a[ , c("x")] <- apply(a[ , c("x")], 1, function(z){replace(z, is.na(z), 0)});
a$x[is.na(a$x), ] <- 0;

None of the above methods replaced NA with 0 in column x for data.frame a. Why?

  • 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-13T19:34:46+00:00Added an answer on June 13, 2026 at 7:34 pm

    Since nobody so far felt fit to point out why what you’re trying doesn’t work:

    1. NA == NA doesn’t return TRUE, it returns NA (since comparing to undefined values should yield an undefined result).
    2. You’re trying to call apply on an atomic vector. You can’t use apply to loop over the elements in a column.
    3. Your subscripts are off – you’re trying to give two indices into a$x, which is just the column (an atomic vector).

    I’d fix up 3. to get to a$x[is.na(a$x)] <- 0

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

Sidebar

Related Questions

Possible Duplicate: Set Jtable/Column Renderer for booleans Right now I have in my Jtable:
Possible Duplicate: How do you set a default value for a MySQL Datetime column?
Possible Duplicate: Set/Get values for RadioGroupFieldEditor in SWT I have a RadioGroupFieldEditor field and
Possible Duplicate: write.csv() a list of unequally sized data.frames I have a data frame
Possible Duplicate: SmartGWT Dialog title not set correctly I have a problem, is that
Possible Duplicate: remove_if equivalent for std::map I have a set of strings: set <wstring>
Possible Duplicate: Retrieving the last record in each group I have two tables set
Possible Duplicate: Android - how to set the wallpaper image I want to change
Possible Duplicate: dropping factor levels in a subsetted data frame in R I'm trying
Possible Duplicate: Is there a “Set” data structure in .Net? Duplicate: this is a

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.