2
0

functions.R 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # FUNCTION FILE
  2. #
  3. # THIS FILE CONTAINS ALL THE FUNCTIONS THAT MIGTH BE USED GLOBALLY
  4. # specify_decimal. Set how many decimals an number should have
  5. # x => numeric
  6. # k => integer
  7. #
  8. # Returns => numeric with {k} decimals
  9. specify_decimal <- function(x, k) format(round(x, k), nsmall=k)
  10. # consistent. Check if a vactor contains all values between the start and the end
  11. # a => vector containing integers
  12. #
  13. # Returns => bool, true contains all values/ false not
  14. consistent <- function(a) suppressWarnings(all(a == min(a):max(a)))
  15. # group. Makes groups of consistent integer vectors
  16. # a => vector containing integers
  17. # n => integer defining minimum group size
  18. #
  19. # Returns => list, containing consistent integer vectors
  20. group <- function(a, n=1){
  21. a <- sort.int(a, decreasing = FALSE)
  22. a <- unique(a)
  23. if(consistent(a)) return (list(A=a))
  24. else{
  25. l_ <- list()
  26. cur_ <- 1
  27. for(i in a){
  28. if(is.null(l_$A)){ # Create first group
  29. l_$A <- c(i)
  30. next
  31. }
  32. if(max(l_[[cur_]])+1 == i) l_[[cur_]] <- c(l_[[cur_]], i) # Add to group
  33. else{ # Create new group
  34. cur_ <- cur_ + 1
  35. l_[[LETTERS[cur_]]] <- c(i)
  36. }
  37. }
  38. cur_ <- 1
  39. while(cur_ <= length(l_)){
  40. if(length(l_[[cur_]]) < n){
  41. l_[[cur_]] <- NULL
  42. cur_ <- 0
  43. }
  44. cur_ <- cur_ + 1
  45. }
  46. names(l_) <- LETTERS[1:length(l_)]
  47. remove(cur_)
  48. return(l_)
  49. }
  50. }