Have you considered a "for" loop, something like the following:
n.id <- dim(Dataframe1)
DF12 <- data.frame(Dataframe1, labdate=rep(NA,n.id), CD4=rep(NA, n.id))
for(i in 1:n.id){
df2.i <- Datafram2[Datafram2$id==DF12$id[i],]
...
DF12$labdate[i] <- ...
DF12$CD4[i] <- ...
}
Alternatively, what about something like the following:
DF12m <- merge(Dataframe1, Datafram2)
DF12m$d.t <- abs(DF12m$fudate - DF12m$labdate)
DF12n <- DF12m[order(DF12m$id, DF12m$d.t),]
N12 <- dim(DF12n)[1]
DF12 <- DF12n[c(T, DF12n$id[-1]==DF12n$id[-N12],]
I have not tested either of these, but I've gotten similar code to work
in the past.
hth. spencer graves
Pedram Sendi wrote:
Dear All,
I have 2 data frames, in dataframe 1 each patient is contained in one row,
defined by a unique patient id and a follow-up date; in data set 2, there
are multiple rows for the same patient with different labdates and
labresults (CD4), e.g.
Dataframe 1
id fupdate
1 01/02/2003
2 03/02/2003
3 05/03/2003
4 06/04/2003
Datafram 2
id labdate CD4
1 date1 200
1 date2 150
1 etc. 300
1
2
2
2
2
2
3
3
3
4
4
4
4
I would like to include for each patient as an additional column in
Dataframe 1 that single labdate (inlcuding the corresponding CD4 count and
other potential variables in the same row) of Dataframe 2 which is closest
(within three months difference) to the fupdate in Dataframe 1.
Any hints how to do this in s-plus is greatly appreciated.
Thanks in advance,
Pedram
Pedram Sendi, MD, DSc
Institute for Clinical Epidemiology & Division of Infectious Diseases
Institut fur klinische Epidemiologie & Abteilung fur Infektiologie
Basel University Hospital
Kantonsspital Basel
Hebelstrasse 10, 3rd Floor
CH-4031 Basel
Switzerland
Phone: +41 61 265 31 02
Fax: +41 61 265 31 09
E-Mail: psendi@uhbs.ch
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|