r - How to create a new ordinal variable according to other variables? -


hi members of community. question might seems identical asked time before, may repetition request output different previous question.

i have 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 create new variable identify how many burst (a burst defined cycle of observation within each day) recorded each id, this:

in$bursttrue<-rep(c(rep(1,10),rep(2,10)),3) 

until have try solution (but unfortunately doesn't work because correctly identifies each burst, not according each id).

in$burst<-with(in,as.numeric(interaction(in$id,in$date,lex.order=true))) 

i suppose function ave useful solve task: tried several combinations none work report solution more near request output. suggestion appreciate!

first create data.frame in parameter stringsasfactors=false 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 want aggregation (like simon's answer) can accomplished using ave by:

unique(within(in, { bla <- ave(date, list(id,date), fun=length)})) 

alternatively, using table shown under comments:

as.data.frame(table(in$id, in$date)) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -