| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- # FUNCTION FILE
- #
- # THIS FILE CONTAINS ALL THE FUNCTIONS THAT MIGTH BE USED GLOBALLY
- # specify_decimal. Set how many decimals an number should have
- # x => numeric
- # k => integer
- #
- # Returns => numeric with {k} decimals
- specify_decimal <- function(x, k) format(round(x, k), nsmall=k)
- # consistent. Check if a vactor contains all values between the start and the end
- # a => vector containing integers
- #
- # Returns => bool, true contains all values/ false not
- consistent <- function(a) suppressWarnings(all(a == min(a):max(a)))
- # group. Makes groups of consistent integer vectors
- # a => vector containing integers
- # n => integer defining minimum group size
- #
- # Returns => list, containing consistent integer vectors
- group <- function(a, n=1){
- a <- sort.int(a, decreasing = FALSE)
- a <- unique(a)
- if(consistent(a)) return (list(A=a))
- else{
- l_ <- list()
- cur_ <- 1
- for(i in a){
- if(is.null(l_$A)){ # Create first group
- l_$A <- c(i)
- next
- }
- if(max(l_[[cur_]])+1 == i) l_[[cur_]] <- c(l_[[cur_]], i) # Add to group
- else{ # Create new group
- cur_ <- cur_ + 1
- l_[[LETTERS[cur_]]] <- c(i)
- }
- }
- cur_ <- 1
- while(cur_ <= length(l_)){
- if(length(l_[[cur_]]) < n){
- l_[[cur_]] <- NULL
- cur_ <- 0
- }
- cur_ <- cur_ + 1
- }
- names(l_) <- LETTERS[1:length(l_)]
- remove(cur_)
- return(l_)
- }
- }
- # Console.NewLine. Write string to console and appends a new line
- # a => string to output
- #
- # Returns => void, but prints in console with appended newline
- Console.NewLine <- function(a){
- Console.Line(a)
- cat("\n")
- }
- # Console.Line. Write string to console
- # a => string to output
- #
- # Returns => void, but prints in console
- Console.Line <- function(a){
- cat(a)
- }
|