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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:49:22+00:00 2026-06-14T10:49:22+00:00

I have recently started using R and although I have a manual for it

  • 0

I have recently started using R and although I have a manual for it I keep on finding that the functions that I need can’t be found there. Here is a problem I stumbled upon.

MY data looks something like this:

col1    col2    col3
Alex    NA  URL
Mike    URL NA
John    URL URL
Peter   NA  NA
James   NA  URL

Col1 will always be a unique categorical value. Col2 represents the source where these people are coming from, to my website (URL means that there is an entire URL in there , could http.www.facebook.com). NA means that the user came to my website viral. Col3 represents the referal (another indication of where the user came from).

What I need to do is to transfer or copy data from col3 into col2, based on the following condition: IF in col 3 I have an URL and in col2 I have NA, then the cell with the URL from col3 I need it copyied to col2. If col3 and col2, both have URL then I don’t want anything to happen there. If col 3 has NA and col2 has URL, again I don’t want anything to change. Here is the desired output

col1    col2                    col3   
Alex    URL(copied from col3)   URL
Mike    URL(kept this URL)      NA
John    URL(kept this URL)      URL
Peter   NA(Kept NA)             NA
James   URL(copied from col3)   URL

SO, Alex and James got the URL from col3 , John and Mike kept their initial URL that were in col2 and Peter kept his NA.

Now, I have looked everywhere , even on this website and was unable to find anything about copying data from one column to another using “IF” conditions. The only thing I found was how to copy an entire column from one dataframe to another by using the “merge” function, but apart from that nothing else.

Does a function exist that could acomplish this?

  • 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-14T10:49:23+00:00Added an answer on June 14, 2026 at 10:49 am

    Your example is not reproducible, so I’ll have to create some of my own:

    dat = data.frame(name = sample(c("John", "James", "Peter"), size = 10, replace = TRUE),
                     source = sprintf("http://www.%s.com", sample(LETTERS, size = 10)),
                     referal = sprintf("http://www.%s.com", sample(LETTERS, size = 10)))
    # Introduce some NA's
    dat[c(1,3,9), "source"] <- NA
    dat[c(2,7), "referal"] <- NA
    > dat
        name           source          referal
    1   John             <NA> http://www.W.com                          
    2  James http://www.M.com             <NA>                          
    3   John             <NA> http://www.Z.com                          
    4  Peter http://www.J.com http://www.L.com                          
    5  Peter http://www.L.com http://www.H.com                          
    6  Peter http://www.T.com http://www.U.com                          
    7  James http://www.E.com             <NA>                          
    8  Peter http://www.K.com http://www.K.com                          
    9  Peter             <NA> http://www.R.com                          
    10 James http://www.Z.com http://www.N.com 
    

    The function you are looking for is called ifelse:

    dat = within(dat, { 
          source = as.character(source)
          referal = as.character(referal)
          source = ifelse(is.na(source), referal, source) 
        } )
    > dat
        name           source          referal
    1   John http://www.W.com http://www.W.com                          
    2  James http://www.M.com             <NA>                          
    3   John http://www.Z.com http://www.Z.com                          
    4  Peter http://www.J.com http://www.L.com                          
    5  Peter http://www.L.com http://www.H.com                          
    6  Peter http://www.T.com http://www.U.com                          
    7  James http://www.E.com             <NA>                          
    8  Peter http://www.K.com http://www.K.com                          
    9  Peter http://www.R.com http://www.R.com                          
    10 James http://www.Z.com http://www.N.com   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started using coffeescript with Rails and I am finding that sometimes
I have recently started to learn Objective-C and write my tests using OCUnit that
I've started using npm for JavaScript package management recently. Although I do have a
I have recently started using generics in java, and in that attempt tried to
I have recently started using SASS in my web development. Thus far, I've found
I have recently started using Netbeans with following stack Liferay, icefaces with jBoss. Is
I have recently started using RSpec for Integration testing in my Rails application, to
I have recently started using DocCheck for checking the validity of JavaDoc's in code
We have recently started using FxCop on our code base and I am in
I have recently started using Zend Studio which has reported as warning the following

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.