Skip to content

Instantly share code, notes, and snippets.

@stephenturner
Created February 17, 2011 21:21
Show Gist options
  • Save stephenturner/832732 to your computer and use it in GitHub Desktop.
Save stephenturner/832732 to your computer and use it in GitHub Desktop.
permute_column.r
# permutes a column in a data.frame, sets seed optionally
permute <- function (dataframe, columnToPermute="column", seed=NULL) {
if (!is.null(seed)) set.seed(seed)
colindex <- which(names(dataframe)==columnToPermute)
permutedcol <- dataframe[ ,colindex][sample(1:nrow(dataframe))]
dataframe[colindex] <- permutedcol
return(dataframe)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment