Try this:
library(gsubfn)
# convert date strings to dd-mm-yy
Dates <- gsub("/", "-", dat$Dates)
# regular expressiont to match dates
re <- "\\d\\d-\\d\\d-\\d\\d"
# extract dates and convert them to Date class
# giving a list d each of whose components is a vector of dates
d <- strapply(Dates, re, ~ as.Date(x, "%d-%m-%y"), perl = TRUE)
names(d) <- dat$ID
# combine them into a single data frame
do.call(rbind, lapply(dat$ID, function(id) data.frame(ID = id, Date = d[[id]])))
On Fri, Mar 6, 2009 at 6:17 AM, bartjoosen <bartjoosen at hotmail.com>
wrote:>
> Hi,
>
> I have the following dataframe:
> ? ID ? ? ? ? ? ? ? ? ? ? ? ? ? ?Dates
> ?1 ? ? ? ? ? ? ? 16-07-01 ?06-10-95
> ?2 ? ? ? ? ? ?24/01/02 ? ? 06-10-95
> ?3 16/01/02 ? 16/08/94 ? ? 12/01/91
>
> And I would like to extract the dates, but couple the ID's to the right
> dates, eg:
> ID ?Dates
> 1 ? ?16-07-01
> 1 ? ?06-10-95
> 2 ? ?24-01-02
> 2 ?....
>
> I have no clue about how to get started, looks something for the
> regexp/grep/... kind of functions, but I don't get the point.
>
> Any thoughts??
>
> Kind regards
>
> Bart Joosen
>
> PS: for the reconstruction of the dataframe:
> dat <-
> structure(list(ID = c(1, 2, 3), Dates = structure(c(2L, 4L, 6L
> ), .Label = c("16-01-02 ? 16-08-94", "16-07-01
?06-10-95", "24-01-02
> 06-10-95",
> "24/01/02 ? ? 06-10-95", "16/01/02 ? 16/08/94",
"16/01/02 ? 16/08/94
> 12/01/91"
> ), class = "factor")), .Names = c("ID",
"Dates"), row.names = c(NA,
> 3L), class = "data.frame")
>
>
> --
> View this message in context:
http://www.nabble.com/Extract-dates-from-dataframe-tp22369479p22369479.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.
>