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)
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)
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!
You could use
duplicated
for this:Here is a way to do it with
plyr
:Note that the rows have been reordered.
Another solution with
data.table
:There is no reordering here.
Or using base
ave
: