Find all indices of duplicates and write them in new columns

Viewed 430

I have a data.frame with a single column, a vector of strings.

These strings have duplicate values. I want to find the character strings that have duplicates in this vector and write their index of position in a new column.

So for example consider I have:

DT<- data.frame(string=A,B,C,D,E,F,A,C,F,Z,A)

I want to get:

string match2 match2 match3 matchx....
A      1       7      11
B      2       NA     NA
C      3       8      NA
D      4       NA     NA
E      5       NA     NA
F      6       9      NA
A      1       7      11
C      3       8      NA
F      6       9      NA
Z      10      NA     NA
A      1       7      11

The string is ways longer than in this example and I do not know the amount of maximum columns I need.

What will be the most effective way to do this? I know that there is the duplicate function but I am not exactly sure how to combine it to the result I want to get here.

Many thanks!

3 Answers
Related