2
0

functions.R 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  51. # Console.NewLine. Write string to console and appends a new line
  52. # a => string to output
  53. #
  54. # Returns => void, but prints in console with appended newline
  55. Console.NewLine <- function(a){
  56. Console.Line(a)
  57. cat("\n")
  58. }
  59. # Console.Line. Write string to console
  60. # a => string to output
  61. #
  62. # Returns => void, but prints in console
  63. Console.Line <- function(a){
  64. cat(a)
  65. }