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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:17:47+00:00 2026-06-15T05:17:47+00:00

I have some data like these var1 var2 10 NA 101 NA NA 86

  • 0

I have some data like these

var1   var2
10     NA
101    NA
NA     86
11     NA
NA     11
NA     61

If one variable is NA then the other one is not, and vice-versa.

How can I combine them into a single variable:

var3
10
101
86
11
11
61

I can do it easily with a loop, but it is quite slow, so I would like to find an easier way. I thought about assigning 0 to the values that are NA and then just adding the variables together…is there a better way ?

  • 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-15T05:17:49+00:00Added an answer on June 15, 2026 at 5:17 am

    Various methods exist. Here’s one way:

    var3 <- ifelse(!is.na(var1),var1,var2)
    

    Here it is working on your example:

      var1 <- c(10,101,NA,11,NA,NA)
      var2 <- c(NA,NA,86,NA,11,61)
    
      var3 <- ifelse(!is.na(var1),var1,var2)
    
     > var3
     [1]  10 101  86  11  11  61
    

    This method is relatively general – it works with non-numeric data for example:

      var1 <- c("AB","WZ",NA,"MN",NA,NA)
      var2 <- c(NA,NA,"QT",NA,"MN","RS")
    
      var3 <- ifelse(!is.na(var1),var1,var2)
    
     > var3
     [1] "AB" "WZ" "QT" "MN" "MN" "RS"
    

    The suggestion of replacing NA with 0 and adding wouldn’t work in that case.

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

Sidebar

Related Questions

I have some data represented as multi-dimensional arrays. I would like to embed these
I have some data like this : 1 TC1 PASS 2 TC2 FAIL 3
I have some data like this: -1,2752 -1,4735 1 -0,5500 1,3287 2 -1,6293 -2,1460
I have some data formated like the following 2009.07.02 02:20:14 40.3727 28.2330 6.4 2.6
I have some data which look like that: PMID- 19587274 OWN - NLM DP
I have some data that I would like to visualize. Each byte of the
I have some data that looks something like this... +----------+----------+----------+ | Column 1 |
I have some data from log files and would like to group entries by
I have some data in a table that looks roughly like the following: table
I have some data in an XML element that looks like this: <?xml version=1.0

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.