Generating a variable that correctly labels each subject's records as their first, second, etc. visit according to a visit date

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 the chron package's dates() function in order to format the visit date column as desired.

datta <- data.frame(id = sample(1:5, size = 20, replace = TRUE), 
   visitdate = sample(seq.dates(from = "1/1/2001", to = "1/1/2007", by = "days"), 
   size = 20, replace = TRUE))
datta <- upData(datta,
   visitdate = dates(as.character(visitdate), format = "m/d/y", out.format = "m/d/y"))

If you look at the resulting 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.

len <- function(vec) {

Lastly, we can use the tapply() function to apply the len() function to the records of each subject ID.

datta$visits <- unlist(tapply(datta$id, INDEX = datta$id, FUN = len))
Topic revision: r2 - 15 Nov 2006, TheresaScott

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback