简体   繁体   中英

Distinct values for each group in R

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.

as mentionend in the comments by Edo, you can use summarize or summarise

library(dplyr)
df_SUMMARY <- df %>%
  group_by(CODE, GROUP)  %>%
  summarize(mean_PRICE = mean(PRICE))

> df_SUMMARY
# A tibble: 8 × 3
# Groups:   CODE [5]
   CODE GROUP mean_PRICE
  <dbl> <chr>      <dbl>
1   320 4T          5.64
2   343 3T          7.99
3   349 1T          0.64
4   349 3T          3.9 
5   602 1T          1.27
6   622 1T          1.96
7   622 2T          5.13
8   622 4T          0.85

``

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM