Skip to content

Instantly share code, notes, and snippets.

@luisDVA
Created June 27, 2017 21:12
Show Gist options
  • Save luisDVA/52d4e3d6eb3cf096ccff5ac2a26cce6b to your computer and use it in GitHub Desktop.
Save luisDVA/52d4e3d6eb3cf096ccff5ac2a26cce6b to your computer and use it in GitHub Desktop.
vectorized fn to expand a series (consecutive IDs implied)
# expanding a series, consecutive values
# by Filipe Chichorro, modified for vectors
expandSeries <- function(strStartEnd){
words<-strsplit(strStartEnd, " ")
if (length(words[[1]])== 1) return(strStartEnd)
else
start_str<-words[[1]][1]
end_str<-words[[1]][3]
start_split<-strsplit(start_str, "[.]")[[1]]
end_split<-strsplit(end_str, "[.]")[[1]]
start_nr<-as.integer(start_split[length(start_split)])
end_nr<-as.integer(end_split[length(end_split)])
base<-substr(start_str, 1, nchar(start_str)- #fixed
nchar(start_split[length(start_split)]))
final_str<-start_str
for (i in ((start_nr + 1):end_nr)){
final_str<-c(final_str, paste(base, i, sep=""))
}
final_strC <- paste(final_str,collapse = ", ")
return(final_strC)
}
expandSeries <- Vectorize(FUN = expandSeries,USE.NAMES = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment