[英]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.