Reputation: 33
I have a data table which has variable Top and I have considered Top variable as my row names as you can see the first table the row names are repeated ones, so I want to add indexing to the row names like sub indexing based on the position of the occurrence.
First table
row Top points
IS IS 3
HT HT S
ON2 ON2 837
IS IS 19
NO NO 41
IS IS IC
ON2 ON2 40
HT HT 1
ON2 ON2 BI
Expected - If I want to see the row name ON2_1 it has appeared in three places and I have to index them based on the position they appeared.
output
rowname occured Top points
IS_1 1ST TIME IS
HT_1 1ST TIME HT
ON2_1 1ST TIME ON2
IS_2 2ND TIME IS
NO_1 1ST TIME NO
IS_3 3RD TIME IS
ON2_2 2ND TIME ON2
HT_2 2ND TIME HT
ON2_3 3RD TIME ON2
Upvotes: 0
Views: 43
Reputation: 887028
We could use
library(dplyr)
df1 %>%
group_by(Top) %>%
mutate(rn = row_number()) %>%
transmute(rowname = paste(row, rn , sep="_"),
occured = c("1ST", "2ND", "3RD")[rn],
points = row) %>%
ungroup %>%
mutate(Top = "TIME")
Upvotes: 1