# your code
Subject<- c("2", "2", "2", "3",
"3", "3", "4", "4", "5",
"5", "5", "5")
dates <- seq(as.Date('2011-01-01'),as.Date('2011-01-12'), by
= 1)
deps <- c("A", "B", "C", "C",
"D", "A", "F", "G", "A",
"F", "A", "D")
df <- data.frame(Subject, dates, deps)
df
final<-c("2 2011-01-02 B","2 2011-01-03 C","3
2011-01-05 D","3 2011-01-06 A",
"4 2011-01-07 F","4 2011-01-08 G","5
2011-01-10 F","5 2011-01-11 A",
"5 2011-01-12 D")
# here below my code
dep.list <- c("B", "D", "F")
sel.row = NULL
for (dep in dep.list) {
f <- which(df$deps == dep)
sel.row <- c(sel.row, c(f, f+1))
}
sel.row[sel.row > nrow(df)] <- NA
sel.row <- na.omit(sel.row)
df.sel <- df[sel.row,]
df.sel.ord <- df.sel[order(df.sel$dates),]
# showing and comparing with final> df.sel.ord
Subject dates deps
2 2 2011-01-02 B
3 2 2011-01-03 C
5 3 2011-01-05 D
6 3 2011-01-06 A
7 4 2011-01-07 F
8 4 2011-01-08 G
10 5 2011-01-10 F
11 5 2011-01-11 A
12 5 2011-01-12 D
> data.frame(final)
final
1 2 2011-01-02 B
2 2 2011-01-03 C
3 3 2011-01-05 D
4 3 2011-01-06 A
5 4 2011-01-07 F
6 4 2011-01-08 G
7 5 2011-01-10 F
8 5 2011-01-11 A
9 5 2011-01-12 D
--
Best
GG
[[alternative HTML version deleted]]