I am working with the R programming language. Suppose I have the following data:
Data_I_Have <- data.frame(
"Person" = c("John", "John", "John", "Peter", "Peter", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
"Number_of_Kids" = c("4", "1", "1", "5", "2", "3", "7", "0", "3", "3", "5")
)
Person Number_of_Kids
1 John 4
2 John 1
3 John 1
4 Peter 5
5 Peter 2
6 Peter 3
7 Tim 7
8 Kevin 0
9 Adam 3
10 Adam 3
11 Xavier 5
Is it possible to "mark" each duplicate name so that it looks like the file below (e.g. John_1, John_2, etc.)?
Data_I_Want <- data.frame(
"Person" = c("John_1", "John_2", "John_3", "Peter_1", "Peter_2", "Peter_3", "Tim", "Kevin", "Adam_1", "Adam_2", "Xavier"),
"Number_of_Kids" = c("4", "1", "1", "5", "2", "3", "7", "0", "3", "3", "5")
)
Person Number_of_Kids
1 John_1 4
2 John_2 1
3 John_3 1
4 Peter_1 5
5 Peter_2 2
6 Peter_3 3
7 Tim 7
8 Kevin 0
9 Adam_1 3
10 Adam_2 3
11 Xavier 5
Using this previous question Add specific characters to duplicated strings, I tried to follow the method used there:
Data_I_Want <- make.unique(Data_I_Have, sep = '_')
But this gives me the following error:
Error in make.unique(Data_I_Have, sep = "_") :
'names' must be a character vector
Can someone please show me how to fix this?
Thanks!