I doing such a task, where should calculate average PRICE for every CODE in each GROUP and code should be unique in each group. The dataset is large, but there is an output of the dput(head(df, 10)) to have an idea about it:
structure(list(GROUP = c("4T", "1T", "3T", "1T", "4T", "2T", "4T", "3T", "1T", "2T"),
CODE = c(320, 602, 349, 349, 320, 622, 622, 343, 622, 622),
PRICE = c(1.78, 1.27, 3.9, 0.64, 9.5, 8.99, 0.85, 7.99, 1.96, 1.27)),
row.names = c(NA, -10L), class = c("data.table", "data.frame" ))
I tried this code:
library(dplyr)
df <- df %>%
group_by(CODE, GROUP) %>%
mutate(PRICE = mean(PRICE)) %>%
ungroup
The output looks close to the desired one, but for the particular group, one code should appear only once. The result of the code:
GROUP CODE PRICE
<chr> <dbl> <dbl>
1 4T 320 5.64
2 1T 602 1.27
3 3T 349 3.9
4 1T 349 0.64
5 4T 320 5.64
6 2T 622 5.13
7 4T 622 0.85
8 3T 343 7.99
9 1T 622 1.96
10 2T 622 5.13
In this example, two lines in group 2T are identical, so should be left only one. Similarly, in group 4T also are 2 identical lines, so should be left only one such row (code 320). How to add such a condition to my code? It should work for a general case.