Mark duplicate lines in object R with their corresponding number

I would like to list the lines in an object so that lines that appear more than once are marked as "stringX1", "string2", etc.

This will be an example input:

strings <- c("stringQ", "stringW", "stringE", "stringQ")

Expected Result:

stringOut <- c("stringQ1", "stringW1", "stringE1", "stringQ2")

Note that "stringQ" exists twice, so I expect "stringQ1" and "stringQ2".

+4
source share
2 answers

We can use ave

paste0(strings, ave(strings, strings, FUN = seq_along))

Or if we start numbering with repeating elements

make.unique(strings, sep="")
+4
source

You can do this with the dplyrfollowing:

require(dplyr)

strings <- data.frame(string = c("stringQ", "stringW", "stringE", "stringQ"))

strings %>% group_by(string) %>%
  mutate(stringnumber = paste0(string,row_number())) %>%
  ungroup() %>%
  select(stringnumber)

leads to:

# A tibble: 4 x 1
stringnumber
<chr>
1     stringQ1
2     stringW1
3     stringE1
4     stringQ2
+2
source

Source: https://habr.com/ru/post/1690687/


All Articles