Last active
February 26, 2020 12:04
-
-
Save AbdouSeck/1f024fb740fd6d66038f6ce6389023a8 to your computer and use it in GitHub Desktop.
Reaction to the provided answer for Stackoverflow question at https://stackoverflow.com/q/35712196
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
# There is really not a dynamic way of doing what you want. | |
# You will need to expand the dash and generate a sequence from it. | |
# I wrote an example function that converts something like 2-4 into seq(2, 4) | |
expand.dash <- function(dashed) { | |
limits <- unlist(strsplit(dashed, '-')) | |
seq(limits[1], limits[2]) | |
} | |
# We can use the above function inside another function which expands ballrooms | |
expand.ballrooms <- function(txt) { | |
str <- gsub('\\d+\\s*-\\s*\\d+', '%d', txt) | |
dashed_str <- gsub('[a-zA-Z ]+', '', txt) | |
sprintf(str, expand.dash(dashed_str)) | |
} | |
# Use the paste0 function to combine the result from expand.ballrooms | |
paste0(expand.ballrooms('Ballroom 2-10'), collapse=', ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment