Skip to content

Instantly share code, notes, and snippets.

@tchakravarty
Last active February 22, 2016 13:20
Show Gist options
  • Save tchakravarty/8e45d43d171c59404aba to your computer and use it in GitHub Desktop.
Save tchakravarty/8e45d43d171c59404aba to your computer and use it in GitHub Desktop.
R: Convert Excel Column Name into Column Number
#==============================================================================
# purpose: get the column number from the column name in Excel
# author: tirthankar chakravarty
# created: 22nd february 2016
# revised: 22nd february 2016
# comments:
# - based on this SO answer: http://stackoverflow.com/a/1004624/1414455
#==============================================================================
excel_name_to_number = function(name) {
Reduce(function(cumulative, next_element) {
cumulative*26 + utf8ToInt(next_element) - utf8ToInt("A") + 1
}, x = strsplit(name, "")[[1]], init = 0)
}
excel_name_to_number("AAR") # 720
excel_name_to_number("AR") # 44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment