2
0

functions.R 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if(consistent(a)) return (list(A=a))
  22. else{
  23. l_ <- list()
  24. cur_ <- 1
  25. for(i in a){
  26. if(is.null(l_$A)){ # Create first group
  27. l_$A <- c(i)
  28. next
  29. }
  30. if(max(l_[[cur_]])+1 == i) l_[[cur_]] <- c(l_[[cur_]], i) # Add to group
  31. else{ # Create new group
  32. cur_ <- cur_ + 1
  33. l_[[LETTERS[cur_]]] <- c(i)
  34. }
  35. }
  36. cur_ <- 1
  37. while(cur_ <= length(l_)){
  38. if(length(l_[[cur_]]) < n){
  39. l_[[cur_]] <- NULL
  40. cur_ <- 0
  41. }
  42. cur_ <- cur_ + 1
  43. }
  44. names(l_) <- LETTERS[1:length(l_)]
  45. remove(cur_)
  46. return(l_)
  47. }
  48. }