My questing is kinda similar to this question, and is building on this answer, only thing is that my data is long format, not wide, and I would like to keep it that way.
Wondered if there's smart way to calculate the weighted.mean()
shown in this answer, but with long data.
Say my data lookslike this
library(tidyverse)
dft_w <- structure(list(obs = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), education = c("A",
"A", "B", "B", "B", "B", "A", "A"), Item = c("income", "weight",
"income", "weight", "income", "weight", "income", "weight"),
Amount = c(1000L, 10L, 2000L, 1L, 1500L, 5L, 2000L, 2L)), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame")); dft_w
# A tibble: 8 x 4
obs education Item Amount
<int> <chr> <chr> <int>
1 1 A income 1000
2 1 A weight 10
3 2 B income 2000
4 2 B weight 1
5 3 B income 1500
6 3 B weight 5
7 4 A income 2000
8 4 A weight 2
and I would like to get to something like this
# A tibble: 12 x 4
obs education Item Amount
<int> <chr> <chr> <dbl>
1 1 A income 1000
2 1 A weight 10
3 1 A weighted_income 1167.
4 2 B income 2000
5 2 B weight 1
6 2 B weighted_income 1583.
7 3 B income 1500
8 3 B weight 5
9 3 B weighted_income 1583.
10 4 A income 2000
11 4 A weight 2
12 4 A weighted_income 1167.