Note that there are some explanatory texts on larger screens.

plurals
  1. POEfficiency of transforming counts to percentages and index scores
    text
    copied!<p>I currently have the following code that produces the desired results I want (<code>Data_Index</code> and <code>Data_Percentages</code>)</p> <pre><code>Input_Data &lt;- read.csv("http://dl.dropbox.com/u/881843/RPubsData/gd/2010_pop_estimates.csv", row.names=1, stringsAsFactors = FALSE) Input_Data &lt;- data.frame(head(Input_Data)) Rows &lt;-nrow(Input_Data) Vars &lt;-ncol(Input_Data) - 1 #Total population column TotalCount &lt;- Input_Data[1] #Total population sum TotalCountSum &lt;- sum(TotalCount) Input_Data[1] &lt;- NULL VarNames &lt;- colnames(Input_Data) Data_Per_Row &lt;- c() Data_Index_Row &lt;- c() for (i in 1:Rows) { #Proportion of all areas population found in this row OAPer &lt;- TotalCount[i, ] / TotalCountSum * 100 Data_Per_Col &lt;- c() Data_Index_Col &lt;- c() for(u in 1:Vars) { # For every column value in the selected row # the percentage of that value compared to the # total population (TotalCount) for that row is calculated VarPer &lt;- Input_Data[i, u] / TotalCount[i, ] * 100 # Once the percentage is calculated the index # score is calculated by diving this percentage # by the proportion of the total population in that # area compared to all areas VarIndex &lt;- VarPer / OAPer * 100 # Binds results for all columns in the row Data_Per_Col &lt;- cbind(Data_Per_Col, VarPer) Data_Index_Col &lt;- cbind(Data_Index_Col, VarIndex) } # Binds results for completed row with previously completed rows Data_Per_Row &lt;- rbind(Data_Per_Row, Data_Per_Col) Data_Index_Row &lt;- rbind(Data_Index_Row, Data_Index_Col) } colnames(Data_Per_Row) &lt;- VarNames colnames(Data_Index_Row) &lt;- VarNames # Changes the index scores to range from -1 to 1 OldRange &lt;- (max(Data_Index_Row) - min(Data_Index_Row)) NewRange &lt;- (1 - -1) Data_Index &lt;- (((Data_Index_Row - min(Data_Index_Row)) * NewRange) / OldRange) + -1 Data_Percentages &lt;- Data_Per_Row # Final outputs Data_Index Data_Percentages </code></pre> <p>The problem I have is that the code is very slow. I want to be able to use it on dataset that has 200,000 rows and 200 columns (which using the code at present will take around 4 days). I am sure there must be a way of speeding this process up, but I am not sure how exactly. </p> <p>What the code is doing is taking (in this example) a population counts table divided into age bands and by different areas and turning it into percentages and index scores. Currently there are 2 loops so that every value in all the rows and columns are selected individually have calculations performed on them. I assume it is these loops that is making it run slow, are there any alternatives that produce the same results, but quicker? Thanks for any help you can offer. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload