Hi to all the members of the community. This question might seems identical to another that I asked some time before, so may be could be a repetition but the request output is definitely different from the previous question.
I have the following DB:
ID1=rep((1:1),20)
ID2=rep((2:2),20)
ID3=rep((3:3),20)
ID<-c(ID1,ID2,ID3)
DATE1=rep("2013-1-1",10)
DATE2=rep("2013-1-2",10)
DATE=c(DATE1,DATE2)
IN<-data.frame(ID,DATE=rep(DATE,3))
and I would like to create a new variable that could identify how many burst (a burst is defined as a cycle of observation within each day) were recorded for each id, like this:
IN$BURSTTRUE<-rep(c(rep(1,10),rep(2,10)),3)
Until now I have try this solution (but unfortunately it doesn't work because it correctly identifies each burst, but not according to each id).
IN$BURST<-with(IN,as.numeric(interaction(IN$ID,IN$DATE,lex.order=TRUE)))
I suppose that the function ave
could be useful to solve this task: I tried several combinations but none work so I report the solution more near to the request output.
As always any suggestion is greatly appreciate!
First create your data.frame IN
with the parameter stringsAsFactors=FALSE
as follows:
IN <- data.frame(ID,DATE=rep(DATE,3), stringsAsFactors=FALSE)
Then using ave
:
IN <- within(IN, { bla <- ave(DATE, ID, FUN=function(x) as.numeric(factor(x)))})
If you want aggregation
(like simon's answer) that can be accomplished using ave
as well by:
unique(within(IN, { bla <- ave(DATE, list(ID,DATE), FUN=length)}))
Alternatively, using table
as shown under comments:
as.data.frame(table(IN$ID, IN$DATE))