The idea behind this problem is that you have a data set that has repeated measures for each subject, but each subject can have a different number of repeated measures.
In this data set, you have a "visit date" column that has recorded the date of each subject's visit.
What you would like to create is a new column that numbers each record for each subject as their first, second, etc. visit according to their visit date value.
I created a dummy repeated measures data set to illustrate this problem. I used thechron
package's dates()
function in order to format the visit date column as desired.
datta
dummy repeated measures data set, you'll notice that records are in random order --- i.e., each subject's records are not together, and they are not sorted by their visit date. In order for our solution to work, we first need to sort our data set by subject ID and visit date.
datta <- datta[order(datta$id, datta$visitdate), ]
Now we can define a function that we can use in a tapply()
function invocation to properly label each subject's records as their first, second, etc. visit.
tapply()
function to apply the len()
function to the records of each subject ID.