Skip to content
Prev 304774 / 398506 Next

Create a function to number each repeated visit or measurements

Hi,

May be you can also use ?aggregate():
x1<-aggregate(x,list(x),function(x) seq_along(x)-1)
# Group.1??????????????????????????????????????????????????????????? x
#1?????? a????????????????????????????????????????? 0, 1, 2, 3, 4, 5, 6
#2?????? b???????????????????????????????????????????? 0, 1, 2, 3, 4, 5
#3?????? c??????????????????????????????????? 0, 1, 2, 3, 4, 5, 6, 7, 8
#4?????? d???????????????????????????????? 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
#5?????? e 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17
A.K.



----- Original Message -----
From: PIKAL Petr <petr.pikal at precheza.cz>
To: Jon Toledo <tintin_jb at hotmail.com>; "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Tuesday, September 4, 2012 7:07 AM
Subject: Re: [R] Create a function to number each repeated visit or measurements

Hi

You did not provided any suitable data. 

x<-sort(sample(letters[1:5], 50, rep=T))
unlist(lapply(split(x,x), function(x) (1:length(x))-1))

gives you a vector of indices from 0 to n for sorted vector x.

Regards
Petr
______________________________________________
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.