Skip to content

Instantly share code, notes, and snippets.

@obrl-soil
Last active November 1, 2016 11:11
Show Gist options
  • Save obrl-soil/cefe7765586c877990a95e95556c8638 to your computer and use it in GitHub Desktop.
Save obrl-soil/cefe7765586c877990a95e95556c8638 to your computer and use it in GitHub Desktop.
R integer to ordinal string (1 = "1st", etc)
# Sometimes you just want your iterated filenames to look nice.
nth_fun <- function(x) {
x <- abs(x)
if (as.integer(substring(x, seq(nchar(x)), seq(nchar(x))))[nchar(x)] == 1) {
return(paste0(x, 'st'))
} else if (as.integer(substring(x, seq(nchar(x)), seq(nchar(x))))[nchar(x)] == 2) {
return(paste0(x, 'nd'))
} else if (as.integer(substring(x, seq(nchar(x)), seq(nchar(x))))[nchar(x)] == 3) {
return(paste0(x, 'rd'))
} else {
return(paste0(x, 'th'))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment