Note that there are some explanatory texts on larger screens.

plurals
  1. POWeighted means for all columns in R data.frame
    primarykey
    data
    text
    <p>I have a 32x43 data.frame called "allg2", and I recreated a small portion of it here as 5x5 for simplicity:</p> <pre><code>gneiss mylonite syenite sedimentary Catg 0 3 4 0 -105.7 2 90 1 0 -99.7 15 51 0 0 -95.25 6 0 0 0 -90.5 0 3 9 0 -85.45 </code></pre> <p>As requested, a sample calculation: The 'gneiss' column would be wm=(0/21*-105.7)+(2/21*-99.7)+(15/21*-95.25)+(6/21*-90.5)+(0/21*-85.45) </p> <p>I would like a weighted mean for each column (with the values of interest in Catg, and each column as the weights for that column), but each solution to this that I can find relies on coding in all of the column names. Is it possible to do this without such a list? Note: I just realized that I have been flipping the weights and values to weigh the entire time. My attempts:</p> <pre><code>wm=allg2[,lapply(.SD,weighted.mean,w=Catg),by=list(allg2[1,])] Error: unused argument (by = list(allg2[1, ])) </code></pre> <p>I found this idea from <a href="https://stackoverflow.com/questions/14145859/weighted-means-by-group-and-column">this thread</a>, and tried to adapt it to my situation. Is it not selecting the column names because they're not a true row? I don't really know what this is doing, and I tried temoving the by= part, which gives the error</p> <pre><code> Error in lapply(.SD, weighted.mean, w = Catg) : object '.SD' not found </code></pre> <p>Another attempt was based on <a href="https://stackoverflow.com/questions/9864631/weighted-mean-by-row">this thread</a>. "Catg" is in the 43rd column, so I tried organizing the line as such:</p> <pre><code>wm=apply(allg2, 2, function(x) weighted.mean(x[,43], x[,1:42])) Error in x[, 43] : incorrect number of dimensions </code></pre> <p>I really don't understand this error, because my column of weights should be in [,43].</p> <p>I have also tried: </p> <pre><code>mallg=data.matrix(allg2) wm=colWeightedMeans(mallg,allg2$Catg) Error in colWeightedMeans.matrix(mallg, allg2$Catg) : Argument 'w' has negative weights. </code></pre> <p>I'm really at a loss here. Am I making some small error or am I going about this the completely wrong way?</p>
    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.
    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