Last active
August 29, 2015 14:02
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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