因为我不知道如何以正确的方式在文本中描述我的问题,我给你举了一个例子。我'以下数据框:
my.data = data.frame("names" = c("Michael","Thomas","Daniel","Peter","Michael","Thomas","Ruben","Thomas"))
给出:
names
1 Michael
2 Thomas
3 Daniel
4 Peter
5 Michael
6 Thomas
7 Ruben
8 Thomas
我想要的输出是:
names
1 Michael
2 Thomas
3 Daniel
4 Peter
5 Michael.2
6 Thomas.2
7 Ruben
8 Thomas.3
是否有人建议如何以更自动的方式实现更大的数据框架?
提前致谢。
答案 0 :(得分:1)
我们有一些选择
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"
或make.unique
make.unique(as.character(my.data$names))
#[1] "Michael" "Thomas" "Daniel" "Peter" "Michael.1" "Thomas.1" "Ruben" "Thomas.2"