Note that there are some explanatory texts on larger screens.

plurals
  1. POweighted averages of paired observations in R
    primarykey
    data
    text
    <p>Yesterday I posted a question asking how to obtain averages of observations in neighboring columns of a data set:</p> <p><a href="https://stackoverflow.com/questions/13739243/average-pairs-of-columns-in-r">average pairs of columns in R</a></p> <p>Today I realized I actually need weighted averages. I tried modifying the answer above to the revised situation, but still do not understand the family of apply functions well enough to do so easily.</p> <p>I have written code to obtain weighted averages for an example data set below and can probably use that code with my real data. Nevertheless, if someone is able to illustrate how to use the apply family of functions here for weighted averages, that will go a long way, I think, toward improving my understanding and coding proficiency. Regardless, thank you for all past and future help and ideas.</p> <pre><code>x = read.table(text = " site yr1 yr2 yr3 yr4 1 10 15 6 8 2 10 20 30 40 3 5 NA 2 3 4 100 100 NA NA", sep = "", header = TRUE) x weights = read.table(text = " site yr1 yr2 yr3 yr4 1 2 4 1 3 2 2 2 4 2 3 3 2 2 3 4 4 2 2 4", sep = "", header = TRUE) weights x.weights = x * weights numerator &lt;- matrix(NA, ncol=((ncol(x.weights)/2)+1), nrow=nrow(x.weights)) for(i in 1: ((ncol(weights)-1)/2)) { for(j in 1: nrow(weights) ) { numerator[j, 1 ] &lt;- x[j,1] numerator[j,(i+1)] &lt;- sum(c(x.weights[j,(1 + ((i-1)*2 + 1))], x.weights[j,(1 + ((i-1)*2 + 2))]), na.rm = TRUE) } } numerator denominator &lt;- matrix(NA, ncol=((ncol(weights)/2)+1), nrow=nrow(weights)) for(i in 1: ((ncol(weights)-1)/2)) { for(j in 1: nrow(weights) ) { denominator[j, 1 ] &lt;- x[j,1] denominator[j,(i+1)] &lt;- sum(c(weights[j,(1 + ((i-1)*2 + 1))], weights[j,(1 + ((i-1)*2 + 2))]), na.rm = TRUE) } } denominator weighted.ave &lt;- numerator[,2:ncol(numerator)] / denominator[,2:ncol(denominator)] weighted.ave # insert value from x if one of a pair is missing # insert NA if both in a pair are missing adj.weighted.ave &lt;- weighted.ave for(i in 1: ((ncol(x)-1)/2)) { for(j in 1: nrow(x) ) { if( is.na(x[j,(1 + (i-1)*2 + 1)]) &amp; !is.na(x[j,(1 + (i-1)*2 + 2)])) adj.weighted.ave[j,i] = sum(c(x[j,(1 + ((i-1)*2 + 1))], x[j,(1 + ((i-1)*2 + 2))]), na.rm = TRUE) if(!is.na(x[j,(1 + (i-1)*2 + 1)]) &amp; is.na(x[j,(1 + (i-1)*2 + 2)])) adj.weighted.ave[j,i] = sum(c(x[j,(1 + ((i-1)*2 + 1))], x[j,(1 + ((i-1)*2 + 2))]), na.rm = TRUE) if( is.na(x[j,(1 + (i-1)*2 + 1)]) &amp; is.na(x[j,(1 + (i-1)*2 + 2)])) adj.weighted.ave[j,i] = NA } } adj.weighted.ave # [,1] [,2] # [1,] 13.33333 7.50000 # [2,] 15.00000 33.33333 # [3,] 5.00000 2.60000 # [4,] 100.00000 NA </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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