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 8827245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:19:38+00:00 2026-06-14T07:19:38+00:00

I seem to have stumbled upon a predicament in my efforts to learn using

  • 0

I seem to have stumbled upon a predicament in my efforts to learn using R. I have a dataset that looks like this:

col1  /   col2  /   col3   

NA  / 1 /  2

1  /  NA  /2

What I need to do is replace everything that IS NOT NA with another value (see below)

Col1  /  col2  /    col 3

NA / Data / Data

Data / NA/ Data

I’ve looked everywhere on the web, it seems that everybody wants to replace NA‘s with other values, I on the other hand need the exact opposite but was unable to find any command. I even looked into “IF’s” but was only able to find functions that replace values if they are eiter < or = or > to a certain number. I need something that will specify…”IF different from NA, then replace with….”

  • 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-14T07:19:39+00:00Added an answer on June 14, 2026 at 7:19 am

    The data:

    dat <- data.frame(col1 = c(NA, 1), col2 = c(1, NA), col3 = c(2, 2))
    

    To choose all values that are not NA, use the logical operator ! (“not”) together with is.na:

    dat[!is.na(dat)] <- "Data"
    
      col1 col2 col3
    1 <NA> Data Data
    2 Data <NA> Data
    

    The same can also be achieved with the replace function:

    replace(dat, !is.na(dat), "Data")
    

    Another approach is needed if you want to replace the values of factors. This can be achieved with the following command:

    replace(data.frame(lapply(dat, as.character), stringsAsFactors = FALSE),
            !is.na(dat), "Data")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to have broken validates_confirmation_of in my rails app. My model looks like
I am using MFC for gui development and I stumbled upon a function that
I stumbled across pandas and it looks ideal for simple calculations that I'd like
I seem to have found a bug in jQuery UI that duplicates dragged elements
I seem to have hit a snag while building my project using Ant. The
While looking for something totally different the other day, I have stumbled upon two
I seem to have stumbled across a really weird issue when posting a static
I seem to have stumbled into an interesting anomoly with KnockoutJS. I have an
I seem to have stumbled over a problem regarding saving an xml file from
I have stumbled upon the issue when I need to retract html controls I've

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.