简体   繁体   中英

Copy unique values from one column to another R

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?

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   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM