1

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
Jaap
  • 81,064
  • 34
  • 182
  • 193
akaDrHouse
  • 2,190
  • 2
  • 20
  • 29
  • 3
    `library(data.table); rowid(rleid(mydf$DOSE))` is one way. – Frank May 31 '17 at 16:27
  • 1
    @Frank Thank you so much! – akaDrHouse May 31 '17 at 16:40
  • Sort of a dupe: https://stackoverflow.com/q/32788497/ ? I've seen `rowid(rleid(x))` a few times before but can't find a better dupe target... – Frank May 31 '17 at 17:22
  • [Another link that gives a similar clue (i.e. the use of `rowid(rleid(x))`)](https://stackoverflow.com/questions/42537689/assign-unique-id-to-distinct-values-within-group-with-dplyr). – Jaap May 31 '17 at 18:34

0 Answers0