Skip to content

Instantly share code, notes, and snippets.

@jilmun
Created March 9, 2016 15:01
Show Gist options
  • Save jilmun/ac6cd128bae01c042144 to your computer and use it in GitHub Desktop.
Save jilmun/ac6cd128bae01c042144 to your computer and use it in GitHub Desktop.
# returns A-Z or AA-ZZ for up to 26*26=676 unique categories
anonymise <- function(mycats) {
cnt <- length(unique(mycats))
mycats <- factor(mycats, labels=1:cnt)
if (cnt <= 26)
return(LETTERS[mycats])
newcats <- character(0)
for (i in 1:ceiling(cnt/26))
newcats <- c(newcats, paste0(LETTERS[i],LETTERS[1:26]))
return(newcats[mycats])
}
# test use case
x1 <- sample(c(letters[1:26],LETTERS[1:26]), 100, replace=T)
anonymise(x1)
anonymise(c(letters[1:26], LETTERS[1:26], "A","a"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment