Q:

Mark duplicates in a data.frame recursively

Considering a test dataset, 
dat=data.frame(name=c('A','A','B','C','C','C'),val=c(1,1,2,2,3,2))
name val
A   1
A   1
B   2
C   2
C   3
C   2

What would be the most efficient way to get this output
name val
A   1
A-1   1
B   2
C   2
C-1   3
C-2   2

So, just marking the duplicates with a custom identifier.
I could think of marking them with an common identifier using paste(dat[which(duplicated(dat$name)),1],"-1",sep=''), but this will just put "-1" in front of all the duplicated ones. I want if the item appears for the 3rd time, mark it with "-2" and so on.
Cheers

A:

Using make.unique:
transform(dat,name=make.unique(as.character(name),sep="-"))
  name val
1    A   1
2  A-1   1
3    B   2
4    C   2
5  C-1   3
6  C-2   2

A:

library(plyr)

ddply( dat, .(name), function(d) {
    d$newname <- paste( d$name, cumsum(duplicated(d$name)), sep="-" )
    d
  })

Result:
  name val newname
1    A   1     A-0
2    A   1     A-1
3    B   2     B-0
4    C   2     C-0
5    C   3     C-1
6    C   2     C-2

A:

It's not exactly what you asked for, but you can try this:
within(dat, {
  Name <- paste(name, as.numeric(ave(as.character(name), 
                                     name, FUN = seq_along)) - 1,
                sep = "-")
  rm(name)
})
#   val Name
# 1   1  A-0
# 2   1  A-1
# 3   2  B-0
# 4   2  C-0
# 5   3  C-1
# 6   2  C-2

Or, with a little modification:
within(dat, {
  name <- as.character(name)
  Name <- as.numeric(ave(name, name, FUN = seq_along)) - 1
  Name <- ifelse(Name == 0, name, paste(name, Name, sep = "-"))
  rm(name)
})
#   val Name
# 1   1    A
# 2   1  A-1
# 3   2    B
# 4   2    C
# 5   3  C-1
# 6   2  C-2

