1 year ago
#360647
peprika
calculate weighted.mean() for X variable with two W variables
I have an issue where I am trying to transform a data frame into a summarized data frame with total sums and weighted averages based on multiple columns...
Below is an example of what I am doing...
df <- data.frame('title' = c("X", "Y", "Z", "X", "Y", "Z"),'date' = c("2020-01-01", "2020-01-01", "2020-01-01", "2020-01-02", "2020-01-03", "2020-01-02"),'weight1' = c(84024, 54241, 106601, 65382, 337007, 687682),'weight2' = c(30, 30, 30, 15, 15, 15),'metric1' = c(3.08, 0.964, 0.839, 1.60, 0.839, 0.648),'metric2' = c(588.03, 298.26, 13.95, 104.29, 10.51, 72.53))
agg = df %>%
group_by(df$date, df$title) %>%
summarise(total_weight1 = sum(weight1, na.rm=TRUE), # total sum
total_weight1 = sum(weight2, na.rm=TRUE), # total sum
mean_metric1 = weighted.mean(x = metric1, w = c(weight1, weight2), na.rm=T), # weighted avg
mean_metric2 = weighted.mean(x = metric2, w = c(weight1, weight2), na.rm=T)) # weighted avg
But of course, I get an error message:
Error in
summarise():
Problem while computing
mean_metric1 = weighted.mean(x = metric1, w = c(weight1, weight2), na.rm = T). The error occurred in group 1: df$date = "2020-01-01", df$title = "X".
Caused by error in
weighted.mean.default():
! 'x' and 'w' must have the same length
Is it possible to calculate the weighted average of a value based on two columns? I've tried finding other sources to validate this calculation manually but have failed...
I'm also not sure if calculating this all in the same data frame is the right way to go. Any guidance would be appreciated.
I've tried other sources and functions including rowwise() and mutate() but I keep running into the same issue that my 'x' and 'w' need to have the same length...
r
dplyr
weighted-average
0 Answers
Your Answer