Here is one possible way (I think - untested code)
cData <- do.call(rbind, lapply(split(data, data$prochi),
function(dat) {
dat <- dat[order(dat$date), ]
while(any(d <- (diff(dat$date) <= 3)))
dat <- dat[-(min(which(d))+1), ]
dat
}))
(It would be courteous of you to give us your real name, by the way)
Bill Venables.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Newbie19_02
Sent: Wednesday, 9 March 2011 9:20 PM
To: r-help at r-project.org
Subject: [R] Cleaning date columns
Hi Everyone,
I have the following problem:
data <- structure(list(prochi = c("IND1", "IND1",
"IND1",
"IND2", "IND2", "IND2", "IND2",
"IND3",
"IND4", "IND5"), date_admission = structure(c(6468,
6470, 7063, 9981, 9983, 14186, 14372, 5129, 9767, 11168), class =
"Date")),
.Names = c("prochi",
"date_admission"), row.names = c("27", "28",
"21", "86", "77",
"80", "1", "114", "192",
"322"), class = "data.frame")
I have records for individuals that were taken on specific dates. Some of
the dates are within 3 days of each other. I want to be able to clean my
date column and select the earliest of the dates that occur within 3 days of
each other per individual as a single observation that represents the N
observations. So for example:
input:
IND1 1987-09-17
IND1 1987-09-19
IND1 1989-05-04
output:
IND1 1987-09-17
IND1 1989-05-04
I'm not sure where to start with this?
Thanks,
Nat
--
View this message in context:
http://r.789695.n4.nabble.com/Cleaning-date-columns-tp3343359p3343359.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.