burton030 burton030 - 8 days ago 5
R Question

Add number of occurence of a string in a data frame

because I do not know how to describe my problem in a proper way in text I present you an example. I' ve the following data frame:

my.data = data.frame("names" = c("Michael","Thomas","Daniel","Peter","Michael","Thomas","Ruben","Thomas"))


which gives:

names
1 Michael
2 Thomas
3 Daniel
4 Peter
5 Michael
6 Thomas
7 Ruben
8 Thomas


and my desired output is:

names
1 Michael
2 Thomas
3 Daniel
4 Peter
5 Michael.2
6 Thomas.2
7 Ruben
8 Thomas.3


Does anybody have a suggestion how to achieve it in an automated way for a larger data frame?

Thanks in advance.

Answer

We have some options

my.data$names <- with(my.data, sub("\\.1$", "", paste(names, ave(seq_along(names),
        names, FUN=seq_along), sep=".")))
my.data$names
#[1] "Michael"   "Thomas"    "Daniel"    "Peter"     "Michael.2" "Thomas.2"  "Ruben"     "Thomas.3" 

Or the make.unique

make.unique(as.character(my.data$names))
#[1] "Michael"   "Thomas"    "Daniel"    "Peter"     "Michael.1" "Thomas.1"  "Ruben"     "Thomas.2"