Skip to content

Instantly share code, notes, and snippets.

@low-decarie
Created May 5, 2012 03:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save low-decarie/2599463 to your computer and use it in GitHub Desktop.
Save low-decarie/2599463 to your computer and use it in GitHub Desktop.
anonymize a data.frame
#http://stackoverflow.com/questions/10454973/how-to-create-example-data-set-from-private-data-replacing-variable-names-and-l/10458688#10458688
anonym<-function(df){
if(length(df)>26){
LETTERS<-replicate(floor(length(df)/26),{LETTERS<-c(LETTERS, paste(LETTERS, LETTERS, sep=""))})
}
names(df)<-paste(LETTERS[1:length(df)])
level.id.df<-function(df){
level.id<-function(i){
if(class(df[,i])=="factor" | class(df[,i])=="character"){
column<-paste(names(df)[i],as.numeric(as.factor(df[,i])), sep=".")}else if(is.numeric(df[,i])){
column<-df[,i]/mean(df[,i], na.rm=T)}else{column<-df[,i]}
return(column)}
DF <- data.frame(sapply(seq_along(df), level.id))
names(DF) <- names(df)
return(DF)}
df<-level.id.df(df)
return(df)}
@MichaelMBishop
Copy link

Thanks for this! Might as well link back to stackoverflow: http://stackoverflow.com/a/10458688/303400

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment