FROM
xa=aggregate(x$avg,by=list(x$value),FUN=weighted.mean,w=x$weight)
gives me an error
Error in weighted.mean.default (X [[1L]], ...): 'x' and 'w' must be the same length
But
weighted.mean(x$avg,w=x$weight);
works great.
As shown in the old thread of R , you can use by instead of:
by
wt <- c(5, 5, 4, 1)/15 x <- c(3.7,3.3,3.5,2.8) xx <- data.frame(avg=x, value=gl(2,2), weight=wt) by(xx, xx$value, function(x) weighted.mean(x$avg, x$weight))
This is a question about βmillions of ways to infect a catβ, here is the plyr solution (using the data from the @chl example):
plyr
ddply(xx,.(value),summarise, wm = weighted.mean(avg,weight))
Source: https://habr.com/ru/post/895572/More articles:Using dependency injection as an alternative to singleton - javaA portable way to catch signals and report a problem to the user - c ++How to set this soap php header? - soapHow to define a constant in a class constructor? - phpGet all TCP connections opened by an application using C # - c #How to get the number of open TCP connections held by a particular process in .NET. - .netPIVOT with MONTH () - sqlLine order in Java manifest files - javaDetermining MSI version without installing it - c #Opening WebView in the background - androidAll Articles