Created
September 2, 2020 04:17
-
-
Save yaojenkuo/5c1c8c4192b596b87191b9a85ba72479 to your computer and use it in GitHub Desktop.
Extracting distinct periods given stock split dates.
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
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