Skip to content

Instantly share code, notes, and snippets.

@tgirke
Last active October 22, 2015 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgirke/5de4d165399243efe508 to your computer and use it in GitHub Desktop.
Save tgirke/5de4d165399243efe508 to your computer and use it in GitHub Desktop.
Append occurrence counter to entries in a character vector
############################
## appendCounter Function ##
############################
## Author: Thomas Girke
## Last update: 04-Oct-15
## Function to append occurrence counter to entries in character
## vector and return the results as named vector where the
## original data are in the same order in the data slot
## and the counting result in the name slot.
appendCounter <- function(x, sep="_") {
names(x) <- sprintf(paste0("%0", as.character(nchar(length(x))+1), "d"), seq_along(x))
tmp <- sort(x)
f <- table(tmp)
tmp2 <- unlist(sapply(names(f), function(z) paste(z, 1:f[z], sep=sep)))
names(tmp2) <- names(tmp)
names(x) <- tmp2[sort(names(tmp2))]
return(x)
}
## Usage:
# x <- c("a", "b", "c", "b", "h", "c")
# appendCounter(x, sep="_")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment