Skip to content

Instantly share code, notes, and snippets.

@drewconway
Created February 8, 2011 04:35
Show Gist options
  • Save drewconway/815861 to your computer and use it in GitHub Desktop.
Save drewconway/815861 to your computer and use it in GitHub Desktop.
# A function that converts a given integer into its Roman Numeral equivalent
# A function that converts a given integer into its Roman Numeral equivalent
to.RomanNumeral<-function(x) {
if(0 < x & x < 5000) {
x<-as.integer(x)
digits<-c(1000,900,500,400,100,90,50,40,10,9,5,4,1)
numerals<-c("M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I")
digits.numerals<-as.data.frame(cbind(digits,numerals), stringsAsFactors=FALSE)
numeral<-""
for(i in 1:nrow(digits.numerals)) {
while(x >= as.numeric(digits.numerals[i,1])) {
numeral<-paste(numeral,digits.numerals[i,2],sep="")
x<-x-as.numeric(digits.numerals[i,1])
}
}
return(numeral)
}
else {
stop(paste(x,"is invalid. Input must be an integer between 1 and 4,999"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment