issues regarding command by
, weighted.mean
exist none able solving problem. new r , more used data mining language programming.
i have data frame each individual (observation/row) income, education level , sample weight. want calculate weighted mean of income education level, , want result associated each individual in new column of original data frame, this:
obs income education weight incomegroup 1. 1000 10 --> display weighted mean of income education level 2. 2000 b 1 --> display weighted mean of income education level b 3. 1500 b 5 --> display weighted mean of income education level b 4. 2000 2 --> display weighted mean of income education level
i tried:
data$incomegroup=by(data$education, function(x) weighted.mean(data$income, data$weight))
it not work. weighted mean calculated somehow , appears in column "incomegroup" whole set instead of group or 1 group only, don't know. read things regarding packages plyr
or aggregate
not seem interested in.
the ave{stats}
command gives looking simple mean:
data$incomegroup=ave(data$income,data$education,fun = mean)
it cannot used weights.
thanking in advance help!
if use mutate
, can avoid left_join
library(dplyr) df %>% group_by(education) %>% mutate(weighted_income = weighted.mean(income, weight)) # obs income education weight weighted_income # <int> <int> <fctr> <int> <dbl> #1 1 1000 10 1166.667 #2 2 2000 b 1 1583.333 #3 3 1500 b 5 1583.333 #4 4 2000 2 1166.667
Comments
Post a Comment