Skip to content

Instantly share code, notes, and snippets.

@janfait
Created June 8, 2016 11:57
Show Gist options
  • Save janfait/043886b282bd71fdc92d6f1274e26990 to your computer and use it in GitHub Desktop.
Save janfait/043886b282bd71fdc92d6f1274e26990 to your computer and use it in GitHub Desktop.
encryption function which is, well, impossible to decrypt
encrypt <- function(v) {
#get uniques
vU <- unique(v)
vU <- vU[!is.na(vU) & vU!=""]
#apply the encryption over a vector
vE <- sapply(vU, function(x){
oL <- c(LETTERS, letters)
oN <- 0:9
sL <- sample(oL)
sN <- sample(oN)
x<-chartr(paste(oL,collapse=""), paste(sL,collapse=""),x)
x<-chartr(paste(oN,collapse=""), paste(sN,collapse=""),x)
x
})
#create mapping data frame, old unique vector elemetns as o, replacement vector as n
mapping <- data.frame(o=vU,n=vE)
mapping <- rbind(mapping,c(o="",n=""))
#map back to original values
vE[match(v,mapping$o)]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment