Skip to content

Instantly share code, notes, and snippets.

@jbmartin
Last active August 29, 2015 14:02
Show Gist options
  • Save jbmartin/8f42053075620250484b to your computer and use it in GitHub Desktop.
Save jbmartin/8f42053075620250484b to your computer and use it in GitHub Desktop.
Sort data frame by letter column. Handles multiple letter cases A, B, ..., AA, ..., AAA,.... as commonly found in Excel spreadsheets.
SortByLetterColumn <- function(df, column_to_sort_by) {
# Sorts a data frame by a column containing letters. E.g., A, B, C, ..., AA, BB,...
#
# Args:
# df: data frame to be sorted
# column_to_sort_by: (str) column to sort df by
#
# Returns:
# Data frame sorted by specified column.
sorted <- df[order(nchar(df[,column_to_sort_by]), df[,column_to_sort_by]), ]
return(sorted)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment