简体   繁体   English

将唯一值从一列复制到另一列

[英]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. 我最近开始使用R,虽然我有一本手册,但我仍然发现在那里找不到我需要的功能。 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. Col1将始终是唯一的分类值。 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). Col2代表这些人来自我的网站的来源(URL意味着那里有一个完整的URL,http:www.facebook.com)。 NA means that the user came to my website viral. NA表示用户来到我的网站病毒。 Col3 represents the referal (another indication of where the user came from). Col3代表referal(用户来自何处的另一个指示)。

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. 我需要做的是根据以下条件将数据从col3传输或复制到col2:如果在第3列中我有一个URL而在col2中我有NA,那么带有来自col3的URL的单元格我需要将它复制到COL2。 If col3 and col2, both have URL then I don't want anything to happen there. 如果col3和col2都有URL,那么我不想在那里发生任何事情。 If col 3 has NA and col2 has URL, again I don't want anything to change. 如果col 3有NA而col2有URL,我再也不希望有任何改变。 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. 因此,Alex和James从col3获得了URL,John和Mike保留了他们在col2中的初始URL,而Peter保留了他的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. 现在,我已经到处查看,即使在这个网站上也无法找到任何关于使用“IF”条件将数据从一列复制到另一列的信息。 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 : 您正在寻找的功能称为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   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将唯一值从一个数据框中复制到 R 中的另一个 - Copy Unique values from one data frame to another in R R:提取一列中的唯一值与另一列中的值匹配 - R: Extract unique values in one column match by values in another column (R) 如何根据 R 中的另一列和 ID 从一列复制粘贴值 - (R) How to copy paste values from one column based on another column and ID in R 如何在 R 中从第二行开始将值从一列复制到另一列 - How can I copy values from one column to another starting on the second row, in R R:将值从一个 dataframe 中的一列复制到另一个 dataframe 中的几个选定列,用于组 - R: Copy values from a column in one dataframe to several selected columns in another dataframe for groups 如何在匹配R中的其他列时将特定值从一个数据列复制到另一个数据列? - How to copy specific values from one data column to another while matching other columns in R? 如何从另一列的一列中搜索和提取唯一值? - How to search for and extract unique values from one column in another column? 另一列的唯一值的一列的唯一值 - unique values of one column for unique values of another column 根据r中的ID从一列中查找另一列中的值 - Find values from one column in another column according to ID in r 我可以在按 R 中的另一列分组的同时列出一列的唯一值吗? - Can I list the unique values for one column while grouping by another column in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM