Hi,
Try this:
data1<-data.frame(row=seq(1:10),beh=c(1,1,1,2,2,2,1,1,2,2))
data1<-within(data1, {trip.id<- cumsum(c(1,abs(diff(beh)))); Seq<-ave(row,trip.id,FUN=seq)})
?data1
#?? row beh Seq trip.id
#1??? 1?? 1?? 1?????? 1
#2??? 2?? 1?? 2?????? 1
#3??? 3?? 1?? 3?????? 1
#4??? 4?? 2?? 1?????? 2
#5??? 5?? 2?? 2?????? 2
#6??? 6?? 2?? 3?????? 2
#7??? 7?? 1?? 1?????? 3
#8??? 8?? 1?? 2?????? 3
#9??? 9?? 2?? 1?????? 4
#10? 10?? 2?? 2?????? 4
A.K.
----- Original Message -----
From: Lilia Dmitrieva <sealilia at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Wednesday, March 13, 2013 3:05 PM
Subject: [R] Assign the number to each group of multiple rows
Dear R users,
My data have repeating "beh" parameter : 1 or 2 - type of animal behavior
in subsequent locations. I need to assign unique number to each sequence of
locations.
My data is:
data=data.frame(row=seq(1:10),beh=c(1,1,1,2,2,2,1,1,2,2))
attach(data)
data