Simplify the awful R code to tune string tools

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)

+4
source share
2 answers

I think this one-liner should do the job:

 con1.adj <- con1 * rowSums(con2) / rowSums(con1) 
+15
source

Here's another suggestion to make con1 little better.

 rgen <- function(X,mean=10,sd=3){ round(rnorm(n=length(X),mean=mean,sd=sd)) } con1 <- data.frame(apply(con1,2,rgen)) 

Note that the size of your random vector will match your array dimension, and you can pass different mean and sd dynamically, for example. apply(con1,2,rgen,5,2) , which will generate rnorm with mean=5 and sd=2

+2
source

All Articles