Having an integer vector, how would it look like if it would be increased/decreased proportionally? The challenge is to round the appropiate numbers up to the closer integer.
redistribute_int <- function(vector, new_n) {
# Start rounding down
ratio <- new_n / sum(vector)
vector_2 <- (ratio * vector)
vector_3 <- vector_2 %>% floor()
# Then redistribute the leftovers over the numbers
# closer to the next integer
diff_n <- sum(vector_2) - sum(vector_3)
diff_vector <- (vector_2 - vector_3)
higuest_index <-
sort(diff_vector, index.return = TRUE, decreasing = TRUE)[[2]][1:diff_n]
vector_3[higuest_index] <- vector_3[higuest_index] + 1
return(vector_3)
}