如何根据其他变量的条件生成二进制变量? [英] How to generate binary variable according to the conditions of other variables?
问题描述
再一次,我为提出此类问题而道歉,但是R世界是如此之大,以至于我有时迷失了方向,即使我已经阅读了一些与R相关的最好的书. 我有以下数据库
Once again, I apologize for asking this type of question, but the R world is so large that sometime I feel lost, even if I have read some of the best book related with R. I have the following DB
ID=rep((1:3),3)
x<-as.Date("2013-1-1")
y<-as.Date("2013-1-2")
z<-as.Date("2013-1-3")
DATE<-c(x,x,x,y,x,y,z,z,z)
TRAP<-c(1,1,1,3,2,3,2,1,3)
IN<-data.frame(ID,DATE,TRAP)
并且我想根据以下条件生成一个二进制变量(RESULT):如果DATE和TRAP对于不同的ID相同,则RESULT> y否则为RESULT> n,就像这样
and I would like to produce a binary variable (RESULT) according to the following conditions: if the DATE and the TRAP is the same for the different ID, then RESULT>y otherwise RESULT>n, like this
RESULT<-c("y","y","y","y","n","y","n","n","n")
OUT<-cbind(IN,RESULT)
我认为应该使用ifelse
函数,但是我不知道如何为每个ID显式声明相等控制的条件...
一如既往,每个建议都将不胜感激!
I think that the ifelse
function should be used, but I don't know how to explicit the condition of equality controlling for each ID...
As always, every suggestion is greatly appreciated!
推荐答案
这是使用plyr
做到这一点的一种方法:
Here is a way to do it with plyr
:
R> ddply(IN, .(DATE,TRAP), transform, RESULT=ifelse(length(ID)>1,"y","n"))
ID DATE TRAP RESULT
1 1 2013-01-01 1 y
2 2 2013-01-01 1 y
3 3 2013-01-01 1 y
4 2 2013-01-01 2 n
5 1 2013-01-02 3 y
6 3 2013-01-02 3 y
7 2 2013-01-03 1 n
8 1 2013-01-03 2 n
9 3 2013-01-03 3 n
请注意,行已重新排序.
Note that the rows have been reordered.
使用data.table
的另一种解决方案:
Another solution with data.table
:
R> DT <- data.table(IN)
R> DT[,RESULT:=ifelse(.N>1,"y","n"), by=list(DATE,TRAP)]
R> DT
ID DATE TRAP RESULT
1: 1 2013-01-01 1 y
2: 2 2013-01-01 1 y
3: 3 2013-01-01 1 y
4: 1 2013-01-02 3 y
5: 2 2013-01-01 2 n
6: 3 2013-01-02 3 y
7: 1 2013-01-03 2 n
8: 2 2013-01-03 1 n
9: 3 2013-01-03 3 n
这里没有重新排序.
或使用基础ave
:
IN <- within(IN, { RESULT <- ave(TRAP, list(DATE, TRAP),
FUN= function(x) ifelse(length(x) > 1, "y", "n"))})
# ID DATE TRAP RESULT
# 1 1 2013-01-01 1 y
# 2 2 2013-01-01 1 y
# 3 3 2013-01-01 1 y
# 4 1 2013-01-02 3 y
# 5 2 2013-01-01 2 n
# 6 3 2013-01-02 3 y
# 7 1 2013-01-03 2 n
# 8 2 2013-01-03 1 n
# 9 3 2013-01-03 3 n
这篇关于如何根据其他变量的条件生成二进制变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!