Skip to content

Instantly share code, notes, and snippets.

@verajosemanuel
Last active April 30, 2021 12:30
Show Gist options
  • Save verajosemanuel/8204ee12e19b6718669f263b3379d123 to your computer and use it in GitHub Desktop.
Save verajosemanuel/8204ee12e19b6718669f263b3379d123 to your computer and use it in GitHub Desktop.
anonimyze #dataframe in #R
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)
}
colnames(df) <- paste("Variable", seq_len(ncol(df)), sep = "")
# https://git.io/J33K4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment