每年通过销售来计算顶级产品

发布于 2025-01-22 13:35:03 字数 388 浏览 3 评论 0原文

我有几年和副产品的销售数据,假设是这样的:

Year <- c(2010,2010,2010,2010,2010,2011,2011,2011,2011,2011,2012,2012,2012,2012,2012)
Model <- c("a","b","c","d","e","a","b","c","d","e","a","b","c","d","e")
Sale <- c("30","45","23","33","24","11","56","19","45","56","33","32","89","33","12")
df <- data.frame(Year, Model, Sale)

我想要按年来识别前2个产品的代码,并将所有其余产品汇总为“其他”类别。

I have the data about sales by years and by-products, let's say like this:

Year <- c(2010,2010,2010,2010,2010,2011,2011,2011,2011,2011,2012,2012,2012,2012,2012)
Model <- c("a","b","c","d","e","a","b","c","d","e","a","b","c","d","e")
Sale <- c("30","45","23","33","24","11","56","19","45","56","33","32","89","33","12")
df <- data.frame(Year, Model, Sale)

I want the code which identifies the TOP 2 products by years and summarises all the rest products as category "other".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拔了角的鹿 2025-01-29 13:35:03

类似于Akrun的解决方案:其他策略很高:

library(dplyr)

df %>% 
  type.convert(as.is = TRUE) %>% 
  group_by(Year) %>% 
  arrange(desc(Sale), .by_group = TRUE) %>% 
  mutate(Model = ifelse(Model == first(Model, 2), Model, "Other"))
 
# Groups:   Year [3]
    Year Model  Sale
   <int> <chr> <int>
 1  2010 b        45
 2  2010 d        33
 3  2010 Other    30
 4  2010 Other    24
 5  2010 Other    23
 6  2011 b        56
 7  2011 e        56
 8  2011 Other    45
 9  2011 Other    19
10  2011 Other    11
11  2012 c        89
12  2012 a        33
13  2012 Other    33
14  2012 Other    32
15  2012 Other    12

Similar to akrun's solution: Slighlty other strategy:

library(dplyr)

df %>% 
  type.convert(as.is = TRUE) %>% 
  group_by(Year) %>% 
  arrange(desc(Sale), .by_group = TRUE) %>% 
  mutate(Model = ifelse(Model == first(Model, 2), Model, "Other"))
 
# Groups:   Year [3]
    Year Model  Sale
   <int> <chr> <int>
 1  2010 b        45
 2  2010 d        33
 3  2010 Other    30
 4  2010 Other    24
 5  2010 Other    23
 6  2011 b        56
 7  2011 e        56
 8  2011 Other    45
 9  2011 Other    19
10  2011 Other    11
11  2012 c        89
12  2012 a        33
13  2012 Other    33
14  2012 Other    32
15  2012 Other    12
感情废物 2025-01-29 13:35:03

我们可以在desc结束订单中按'Year'安排 ,然后在按'Year'

library(dplyr)
df %>% 
 arrange(Year, desc(Sale)) %>% 
 group_by(Year) %>%
 mutate(Model = case_when(row_number() > 2~ 'other', TRUE ~ Model)) %>%
 ungroup

或另一个 之后根据Row_number更改'模型'的值选项是使用slice_maxwith_ties = true默认情况下)

df %>%
  group_by(Year) %>% 
  mutate(Model =case_when(Model %in% {cur_data() %>% 
     slice_max(n = 2, order_by = Sale) %>%
     pull(Model)} ~ Model, TRUE ~ "other" )
)

We could arrange by 'Year' and 'Sale' in descending order and then change the values of 'Model' based on the row_number after grouping by 'Year'

library(dplyr)
df %>% 
 arrange(Year, desc(Sale)) %>% 
 group_by(Year) %>%
 mutate(Model = case_when(row_number() > 2~ 'other', TRUE ~ Model)) %>%
 ungroup

Or another option is to use slice_max (with_ties = TRUE by default)

df %>%
  group_by(Year) %>% 
  mutate(Model =case_when(Model %in% {cur_data() %>% 
     slice_max(n = 2, order_by = Sale) %>%
     pull(Model)} ~ Model, TRUE ~ "other" )
)
撞了怀 2025-01-29 13:35:03

另一个可能的解决方案(尽管我不确定OP是否正在寻找输出作为我的答案之一,还是其他答案之一):

library(tidyverse)

df %>% 
  group_by(Year) %>% 
  mutate(top = dense_rank(desc(Sale)) %>% as.character,
         top = if_else(top %in% c("1", "2"), top, "Other")) %>% 
  ungroup

#> # A tibble: 15 × 4
#>     Year Model Sale  top  
#>    <dbl> <chr> <chr> <chr>
#>  1  2010 a     30    Other
#>  2  2010 b     45    1    
#>  3  2010 c     23    Other
#>  4  2010 d     33    2    
#>  5  2010 e     24    Other
#>  6  2011 a     11    Other
#>  7  2011 b     56    1    
#>  8  2011 c     19    Other
#>  9  2011 d     45    2    
#> 10  2011 e     56    1    
#> 11  2012 a     33    2    
#> 12  2012 b     32    Other
#> 13  2012 c     89    1    
#> 14  2012 d     33    2    
#> 15  2012 e     12    Other

Another possible solution (although I am not sure whether the OP is looking for an output as the one of my answer or as the one of the other answers):

library(tidyverse)

df %>% 
  group_by(Year) %>% 
  mutate(top = dense_rank(desc(Sale)) %>% as.character,
         top = if_else(top %in% c("1", "2"), top, "Other")) %>% 
  ungroup

#> # A tibble: 15 × 4
#>     Year Model Sale  top  
#>    <dbl> <chr> <chr> <chr>
#>  1  2010 a     30    Other
#>  2  2010 b     45    1    
#>  3  2010 c     23    Other
#>  4  2010 d     33    2    
#>  5  2010 e     24    Other
#>  6  2011 a     11    Other
#>  7  2011 b     56    1    
#>  8  2011 c     19    Other
#>  9  2011 d     45    2    
#> 10  2011 e     56    1    
#> 11  2012 a     33    2    
#> 12  2012 b     32    Other
#> 13  2012 c     89    1    
#> 14  2012 d     33    2    
#> 15  2012 e     12    Other
等你爱我 2025-01-29 13:35:03

您可以从forcats使用fct_lump_n()来折叠级别,但是首先您需要nocount 您的数据。

library(dplyr)
library(tidyr)
library(forcats)
df |> 
  mutate(Sale = as.integer(Sale)) |>
  uncount(Sale) |> 
  group_by(Year) |> 
  mutate(Model = fct_lump_n(Model, n = 2)) |>
  count(Model)

输出

#> # A tibble: 10 x 3
#> # Groups:   Year [3]
#>     Year Model     n
#>    <dbl> <fct> <int>
#>  1  2010 b        45
#>  2  2010 d        33
#>  3  2010 Other    77
#>  4  2011 b        56
#>  5  2011 Other    75
#>  6  2011 e        56
#>  7  2012 d        33
#>  8  2012 Other    44
#>  9  2012 a        33
#> 10  2012 c        89

(不确定为什么2012年保留4组而不是3组)。

You can use fct_lump_n() from forcats to collapse levels, but first you need to uncount your data.

library(dplyr)
library(tidyr)
library(forcats)
df |> 
  mutate(Sale = as.integer(Sale)) |>
  uncount(Sale) |> 
  group_by(Year) |> 
  mutate(Model = fct_lump_n(Model, n = 2)) |>
  count(Model)

Output

#> # A tibble: 10 x 3
#> # Groups:   Year [3]
#>     Year Model     n
#>    <dbl> <fct> <int>
#>  1  2010 b        45
#>  2  2010 d        33
#>  3  2010 Other    77
#>  4  2011 b        56
#>  5  2011 Other    75
#>  6  2011 e        56
#>  7  2012 d        33
#>  8  2012 Other    44
#>  9  2012 a        33
#> 10  2012 c        89

(Not sure why 2012 keeps 4 groups rather than 3).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文