Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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