簡體   English   中英

我們可以使用 R/Dplyr 根據 2 個不同列中的值進行排名嗎?

[英]Can we use R/Dplyr to Rank Based on Values in 2 Different Columns?

使用下面這個簡單的示例,我的目標是創建一個函數,在刪除達到至少 15 個總分的類別后,該函數將根據 Category_Rank(升序)和 Item_Points(降序)創建 current_rank。 這個想法是,一旦我更新了基礎數據,排名(優先級)就會發生變化,並允許重新關注要獲得的項目。

下面是一個示例數據集和我已經開始的代碼。 在刪除該類別並繼續之前,我的代碼似乎成功地過濾掉了滿足最低點閾值 (15) 的類別,但是 current_rank 並沒有像我希望的那樣工作。

sample2 <- tribble(
  ~Category , ~Category_Rank, ~Item, ~Item_Points, ~Points_Obtained,
  'Shelter', 2, 'Tent',       5, 0,
  'Shelter', 2, 'House',     10, 0,
  'Shelter', 2, 'Hotel',     20, 0,
  'Shelter', 2, 'Yurt',       2, 0,
  'Food',    1, 'Protein',    5, 5,
  'Food',    1, 'Fruit',      2, 0,
  'Food',    1, 'Vegetables', 10, 10,
  'Food',    1, 'Water',     20, 0,
  'Clothes',  3, 'Pants',    20, 0,
  'Clothes',  3, 'Shirts',    5, 0,
  'Clothes',  3, 'Socks',    10, 0,
  'Clothes',  3, 'Shoes',     2, 0,
  
  
)

sample2 %>% 
  group_by(Category) %>% 
  mutate(progress = 
           case_when(
             sum(Points_Obtained) >=15 ~ 'Met',
             TRUE ~ 'Not Met')) %>% 
  filter(progress != 'Met') %>% 
  ungroup() %>% 
  mutate(current_rank = order(order(Category_Rank, Item_Points , decreasing = T)))

我想根據這段代碼看到的輸出是

Category    Category_Rank     Item     Item_Points     Points_Obtained     progress     current_rank
Shelter     2                Tent        5               0                 Not Met         3
Shelter     2                House       10              0                 Not Met         2
Shelter     2                Hotel       20              0                 Not Met         1
Shelter     2                Yurt        2               0                 Not Met         4
Clothes     3                Pants       20              0                 Not Met         5
Clothes     3                Shirts      5               0                 Not Met         7
Clothes     3                Socks       10              0                 Not Met         6
Clothes     3                Shoes       2               0                 Not Met         8

current_rank 沒有按預期工作。

有什么想法嗎?

只需將rev()應用於要反向排序的列:

sample2 %>% 
  group_by(Category) %>% 
  mutate(progress = 
           case_when(
             sum(Points_Obtained) >=15 ~ 'Met',
             TRUE ~ 'Not Met')) %>% 
  filter(progress != 'Met') %>% 
  ungroup() %>% 
  mutate(current_rank = order(rev(Category_Rank), Item_Points, decreasing = T))
# A tibble: 8 x 7
  Category Category_Rank Item   Item_Points Points_Obtained progress current_rank
  <chr>            <dbl> <chr>        <dbl>           <dbl> <chr>           <int>
1 Shelter              2 Tent             5               0 Not Met             3
2 Shelter              2 House           10               0 Not Met             2
3 Shelter              2 Hotel           20               0 Not Met             1
4 Shelter              2 Yurt             2               0 Not Met             4
5 Clothes              3 Pants           20               0 Not Met             5
6 Clothes              3 Shirts           5               0 Not Met             7
7 Clothes              3 Socks           10               0 Not Met             6
8 Clothes              3 Shoes            2               0 Not Met             8

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM