Group_by 2 variables and pivot_wider distribution based on 2 others
Performing some calculations on a dataframe and stuck trying to calculate a few percentages. Trying to append 3 additional columns added for %POS/NEG/NEU. E.g., the sum of amount col for all observations w/ POS Direction in both Drew A/total sum of all amounts for Drew **
Name Rating Amount Price Rate Type Direction
Drew A 455 99.54 4.5 white POS
Drew A 655 88.44 5.3 white NEG
Drew B 454 54.43 3.4 blue NEU
Drew B 654 33.54 5.4 blue POS
Drew C 754 54.43 4.3 green POS
Jon A 454 65.23 3.4 blue NEG
Jon B 954 86.34 4.3 blue NEG
Jon B 545 34.54 4.4 green NEG
Jon C 454 65.45 3.4 green POS
Jon C 544 65.55 4.4 blue NEU
Nick A 675 54.33 3.4 white POS
Nick A 565 65.33 3.4 white POS
Nick B 343 54.44 6.4 blue POS
Nick C 656 65.33 4.3 green NEG
Nick C 655 94.44 3.3 green NEU
To Get the Following Output Calculation for POS/NEG/NEU columns
Name Rating sum count percent wm_price wm_rate mode_type POS NEG NEU
Drew A .3735
Drew B .3728
Drew C
Jon A
Jon B
Jon C
Nick A
Nick B
Nick C
Here's what i got so far but im stuck implementing the pivot_wider for calculating/appending the % direction (POS/NEG/NEU) for each rating category of Each Name Any feedback appreciated!
df - df %% group_by(Name, Rating) %%
summarize(sum_rating = sum(Amount),
count = n(),
wm_Price = weighted.mean(Price,Amount),
wm_Rate = weighted.mean(Rate,Amount),
mode_Type = mode(Type)) %%
mutate(pct_rating = sum_rating/sum(sum_rating)) %%
pivot_wider(names_from = Direction, values_from = Amount/sum Amount)
Topic groupby dplyr data-wrangling data-cleaning r
Category Data Science