Last active
August 29, 2015 14:01
-
-
Save gyli/084470b321ff724118e2 to your computer and use it in GitHub Desktop.
Excel column index translator with R
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
#Translate integer into Excel column index, such as: | |
# 1-26 -> A-Z | |
#27-52 -> AA-AZ | |
#53-78 -> BA-BZ | |
#... | |
num.to.LETTERS<-function(num,letter=""){ | |
if(num<=0){ | |
return(NA) | |
}else if(num/26<=1){ | |
return(paste0(LETTERS[num],letter)) | |
}else{ | |
letter<-paste0(LETTERS[ifelse(num%%26==0,26,num%%26)],letter) | |
return( num.to.LETTERS(ifelse(num%%26==0,floor(num/26-1),floor(num/26)),letter) ) | |
} | |
} | |
#Non-recursive | |
num.to.LETTERS2<-function(num){ | |
a<-vector() | |
i=1 | |
rusult<-"" | |
while(num>0){ | |
n = num %% 26 | |
num = floor(num/26) | |
if(n != 0){ | |
a[i]<-LETTERS[n] | |
}else{ | |
a[i]<-LETTERS[26] | |
num=num-1 | |
} | |
i=i+1 | |
} | |
for(i in length(a):1) rusult<-paste0(rusult,a[i]); | |
return(rusult) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment