I want to define a variable that counts from 1 and starts over based on change in another column. I'm sure this has been answered, but I have had a hard time with successful search.
Example Data: mydf:
ID DOSE
1 AC09 0.1
2 AH01 0.1
3 CH01 0.1
4 AB09 1
5 AA09 10
6 BG02 0.1
7 BF02 1
8 AD01 10
9 CD01 10
I've ordered the data such that I want to count the replications and start over based on change in DOSE as follows:
ID DOSE REP
1 AC09 0.1 1
2 AH01 0.1 2
3 CH01 0.1 3
4 AB09 1 1
5 AA09 10 1
6 BG02 0.1 1
7 BF02 1 1
8 AD01 10 1
9 CD01 10 2
I tried using duplicated and lag functions, but cannot get the right syntax.
> mydf$dups <- !duplicated(mydf[,2])
> mydf$reps <- ifelse(mydf$dups == 1, 1, lag(mydf$dups)+1)
> mydf
ID DOSE dups reps
1 AC09 0.1 TRUE 1
2 AH01 0.1 FALSE 2
3 CH01 0.1 FALSE 1
4 AB09 1 TRUE 1
5 AA09 10 TRUE 1
6 BG02 0.1 FALSE 2
7 BF02 1 FALSE 1
8 AD01 10 FALSE 1
9 CD01 10 FALSE 1
mydf <- structure(list(ID = c("AC09", "AH01", "CH01", "AB09", "AA09", "BG02", "BF02", "AD01", "CD01"),DOSE = c("0.1", "0.1", "0.1", "1", "10", "0.1", "1", "10", "10")), row.names=c(1:9),.Names = c("ID", "DOSE"), class = "data.frame")
mydf