Consider the data:

```
set.seed(123)

x <- rbinom(12, 1, .5)  
y <- (x==0) * rexp(12, 1/100)
z <- (x==1) * rexp(12, 1/220)
group <- sample( rep(1:2, each=6) )

d <- data.frame(x, y, z, group)

```

Sorting the data first by `y`, then by `z`

```
d <- d[order(d$y,d$z),]

```

Now within each group, I want to give rank . The following codes work correctly:

```
ds <- split(d, d$group)

ds1 <- ds[[1]]
ds1$rank <- 1:nrow(ds1)

ds2 <- ds[[2]]
ds2$rank <- 1:nrow(ds2)

```

But without splitting the data frame, I want to rank within each group. How can I do that?