Lilia Dmitrieva
2013-Mar-13 19:05 UTC
[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) >datarow beh 1 1 1 2 2 1 3 3 1 4 4 2 5 5 2 6 6 2 7 7 1 8 8 1 9 9 2 10 10 2 I need the output like this: 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 I managed to assign sequence numbers inside of each group:> seq<-sequence(rle(beh)$length)> new<-cbind(data,seq) > newrow beh seq 1 1 1 1 2 2 1 2 3 3 1 3 4 4 2 1 5 5 2 2 6 6 2 3 7 7 1 1 8 8 1 2 9 9 2 1 10 10 2 2 but I can’t assign the numbers to the groups (the parameter "trip.id")... I would appreciate any help. Regards, Lilia [[alternative HTML version deleted]]
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
? row beh
1? ? 1? ? 1
2? ? 2? ? 1
3? ? 3? ? 1
4? ? 4? ? 2
5? ? 5? ? 2
6? ? 6? ? 2
7? ? 7? ? 1
8? ? 8? ? 1
9? ? 9? ? 2
10? 10? ? 2
I need the output like this:
? 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
I managed to assign sequence numbers inside of each group:
> seq<-sequence(rle(beh)$length)
> new<-cbind(data,seq)
> new
? row beh seq
1? ? 1? ? 1? 1
2? ? 2? ? 1? 2
3? ? 3? ? 1? 3
4? ? 4? ? 2? 1
5? ? 5? ? 2? 2
6? ? 6? ? 2? 3
7? ? 7? ? 1? 1
8? ? 8? ? 1? 2
9? ? 9? ? 2? 1
10? 10? 2? 2
but I can?t assign the numbers to the groups (the parameter
"trip.id")...? I
would appreciate any help.
Regards,
Lilia
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
try this:> data=data.frame(row=seq(1:10),beh=c(1,1,1,2,2,2,1,1,2,2)) > data$tripid <- cumsum(c(TRUE, diff(data$beh) != 0)) > datarow beh tripid 1 1 1 1 2 2 1 1 3 3 1 1 4 4 2 2 5 5 2 2 6 6 2 2 7 7 1 3 8 8 1 3 9 9 2 4 10 10 2 4>On Wed, Mar 13, 2013 at 3:05 PM, Lilia Dmitrieva <sealilia@gmail.com> wrote:> 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 > > > row beh > > 1 1 1 > > 2 2 1 > > 3 3 1 > > 4 4 2 > > 5 5 2 > > 6 6 2 > > 7 7 1 > > 8 8 1 > > 9 9 2 > > 10 10 2 > > > I need the output like this: > > 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 > > I managed to assign sequence numbers inside of each group: > > > > seq<-sequence(rle(beh)$length) > > > new<-cbind(data,seq) > > new > > > row beh seq > > 1 1 1 1 > > 2 2 1 2 > > 3 3 1 3 > > 4 4 2 1 > > 5 5 2 2 > > 6 6 2 3 > > 7 7 1 1 > > 8 8 1 2 > > 9 9 2 1 > > 10 10 2 2 > > > > but I can’t assign the numbers to the groups (the parameter "trip.id")... > I > would appreciate any help. > > > > Regards, > > > > Lilia > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]
I forgot the 'seq':> data=data.frame(row=seq(1:10),beh=c(1,1,1,2,2,2,1,1,2,2)) > data$tripid <- cumsum(c(TRUE, diff(data$beh) != 0)) > data$seq <- ave(data$beh, data$tripid, FUN = function(x) seq_along(x)) > datarow beh tripid seq 1 1 1 1 1 2 2 1 1 2 3 3 1 1 3 4 4 2 2 1 5 5 2 2 2 6 6 2 2 3 7 7 1 3 1 8 8 1 3 2 9 9 2 4 1 10 10 2 4 2 On Wed, Mar 13, 2013 at 3:05 PM, Lilia Dmitrieva <sealilia@gmail.com> wrote:> 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 > > > row beh > > 1 1 1 > > 2 2 1 > > 3 3 1 > > 4 4 2 > > 5 5 2 > > 6 6 2 > > 7 7 1 > > 8 8 1 > > 9 9 2 > > 10 10 2 > > > I need the output like this: > > 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 > > I managed to assign sequence numbers inside of each group: > > > > seq<-sequence(rle(beh)$length) > > > new<-cbind(data,seq) > > new > > > row beh seq > > 1 1 1 1 > > 2 2 1 2 > > 3 3 1 3 > > 4 4 2 1 > > 5 5 2 2 > > 6 6 2 3 > > 7 7 1 1 > > 8 8 1 2 > > 9 9 2 1 > > 10 10 2 2 > > > > but I can’t assign the numbers to the groups (the parameter "trip.id")... > I > would appreciate any help. > > > > Regards, > > > > Lilia > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. [[alternative HTML version deleted]]