Skip to content

Instantly share code, notes, and snippets.

@yaojenkuo
Created September 2, 2020 04:17
Show Gist options
  • Save yaojenkuo/5c1c8c4192b596b87191b9a85ba72479 to your computer and use it in GitHub Desktop.
Save yaojenkuo/5c1c8c4192b596b87191b9a85ba72479 to your computer and use it in GitHub Desktop.
Extracting distinct periods given stock split dates.
get_split_date_periods <- function(split_dates) {
n_splits <- length(split_dates)
if (n_splits > 1) {
split_date_periods <- list()
iterable <- c(1, 1:n_splits)
for (i in 1:length(iterable)) {
if (i == 1) {
split_date_periods[[i]] <- sprintf("/%s", split_dates[iterable[i]] - 1)
} else if (i == length(iterable)) {
split_date_periods[[i]] <- sprintf("%s/", split_dates[iterable[i]])
} else {
split_date_periods[[i]] <- sprintf("%s/%s", split_dates[iterable[i]], split_dates[iterable[i + 1]] - 1)
}
}
return(split_date_periods)
} else if (n_splits == 1) {
split_date_periods <- list()
split_date_periods[[1]] <- sprintf("/%s", split_dates - 1)
split_date_periods[[2]] <- sprintf("%s/", split_dates)
return(split_date_periods)
} else {
message("No split date periods.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment