You are here:
Vanderbilt Biostatistics Wiki
>
Main Web
>
Seminars
>
RClinic
>
VisitCountVariable
(15 Nov 2006,
TheresaScott
)
(raw view)
E
dit
A
ttach
---+ 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. <highlight> library(Hmisc) library(chron) 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")) </highlight> 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. </highlight> datta <- datta[order(datta$id, datta$visitdate), ] </highlight> 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. <highlight> len <- function(vec) { 1:length(vec) } </highlight> Lastly, we can use the =tapply()= function to apply the =len()= function to the records of each subject ID. <highlight> datta$visits <- unlist(tapply(datta$id, INDEX = datta$id, FUN = len)) datta </highlight>
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r2
<
r1
|
B
acklinks
|
V
iew topic
|
Edit
w
iki text
|
M
ore topic actions
Topic revision: r2 - 15 Nov 2006,
TheresaScott
Main
Department Home Page
Biostatistics Graduate Program
Vanderbilt University Medical Center
Main Web
Main Web Home
Search
Recent Changes
Changes
Topic list
Biostatistics Webs
Archive
Main
Sandbox
System
Register
|
Log In
Copyright © 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