, dplyr:
require(dplyr)
df %>%
mutate(date = as.Date(date, format = "%d/%m/%Y")) %>%
group_by(locid) %>%
filter(match == "matched" & date < date[match == "index"])
real Date -format, locid, , matched == "matched" Date - .
. , match == "matched" , , <, (, ), , , , , , <=, match == "matched", .
:
df <- structure(list(locid = c(1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 4L, 4L, 4L), match = structure(c(1L, 2L, 2L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 2L, 2L, 2L), .Label = c("index", "matched"), class = "factor"),
date = structure(c(11L, 1L, 7L, 13L, 8L, 3L, 4L, 9L, 10L,
5L, 2L, 12L, 6L), .Label = c("1/09/2013", "1/10/2012", "1/2/2013",
"1/5/2013", "10/2/2013", "10/3/2013", "14/04/2013", "2/4/2013",
"2/5/2013", "3/3/2013", "4/11/2013", "4/3/2013", "7/1/2014"
), class = "factor"), score = c(15L, 23L, 1L, 21L, 12L, 10L,
23L, 10L, 9L, 32L, 15L, 12L, 10L), iid = c(1L, 2L, 3L, 4L,
1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 5L)), .Names = c("locid",
"match", "date", "score", "iid"), class = "data.frame", row.names = c(NA,
-13L))