I know that this code can be made much shorter and more efficient with the help of powerful vector image processing capabilities. I just canโt understand how at the moment ...
The main task is to adjust the cells in each row so that the total amount is forced to correspond to a predetermined number defined by another data frame. Thus, the total population of each region is forced to have a certain value (each row represents a region), while the relations between cells moving from one column to another remain unchanged.
A terrible way to do this (the first cycle is just to create an example data frame, Iโm sure that this can be done better and thatโs all, I just canโt stop using cycles!):
con1 <- array(dim=c(5,3)) set.seed(1066) for(i in 1:ncol(con1)){ con1[,i] <- round(rnorm(n=5,mean=10,sd=3))} con1 <- data.frame(con1) con2 <- data.frame(array(c(8:13, 9:14, 10:15), dim=c(5,3))) apply(con1,1, sum) apply(con2,1, sum) # different row totals con1.adj <- con1 for ( i in 1:nrow(con1)){ con1.adj[i,1] <- con1[i,1] * ( sum(con2[i,]) / sum(con1[i,]) ) con1.adj[i,2] <- con1[i,2] * ( sum(con2[i,]) / sum(con1[i,]) ) con1.adj[i,3] <- con1[i,3] * ( sum(con2[i,]) / sum(con1[i,]) ) } con1.adj <- data.frame(con1.adj) apply(con1.adj,1, sum) # same row totals
(context: dig this code from someone else and have been for many years. It now terribly seems to me that I pulled up a steep learning curve R. I also want the code to return - used by other people. Indeed, enjoying the language and enjoying it even more if I can find a more beautiful way to do this)