-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of Jon Toledo
Sent: Monday, September 03, 2012 10:59 PM
To: r-help at r-project.org
Subject: [R] Create a function to number each repeated visit or
measurements
Hi dear R list members,
I am trying to create a numerical variable that tracks the
visits/entries that a subject has had. For example having a database of
repeated serial measurements that are ordered by subject and time. So I
would get a variable that says 0 for baseline visit/measurement, 1 for
the second etc.
In my case the unique identifier for each subject is AreaID
Will the following code I can number the visits as long as I create as
many lines as visits.
But there is probably a way of coding a function or maybe there is
already a function.
DUP<-rep(0,length(AreaID))DUP[2:length(DUP)]<-
sapply(2:(length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-
1],1,DUP[x]))DUP[3:length(DUP)]<-sapply(3:(
length(AreaID),function(x) ifelse(AreaID[x]==AreaID[x-2],2,DUP[x]))
My Guess was:
AddLag<-function(x) { DUP<-c(rep(0,length(x))) for (i in
1:(max(as.numeric(GraphArtDB$Measure))+2)){
DUP[(i+1):length(x)]<-sapply(((i+1):length(x)),function(x)
ifelse(x[i]==x[x-i],i,DUP[x])) } return(DUP) }
But it didn?t work.
Any suggestions?
Thanks in advance