R语言 [已解决]将最小和最大评级更改为剧集名称

3hvapo4f  于 2023-01-22  发布在  其他
关注(0)|答案(3)|浏览(116)

我想做一个表格,列出剧集中收视率最低和最高的剧集,但我不知道该怎么做,我试图找到一个解决方案,但我失败了,基本上它应该是这样的:
| 系列|平均额定值|最低评级事件|最高评分事件|
| - ------|- ------|- ------|- ------|
| 绝命毒师|九、六|某某|蒂耶|
| 目标|9.0分|中兴通讯|奎兹|
这是我试过的方法

series%>%
    group_by(serie) %>%
    summarise(
      avg=mean(rating),
      Least = min(rating),
      Highest= max(rating))
xn1cxnb4

xn1cxnb41#

由于您没有提供实际数据来测试代码,因此我在iris数据上测试了此代码,其中我尝试按组species获取Sepal.Lengthminmax,并生成最终df
请检查它是否适用于您的数据

df <- iris %>% group_by(Species) %>% 
filter(Sepal.Length==min(Sepal.Length) | Sepal.Length==max(Sepal.Length)) %>% 
  mutate(name=ifelse(row_number()==1, 'min', 'max')) %>% 
  pivot_wider(Species, values_from = Sepal.Length, names_from = name)

创建于2023年1月21日,使用reprex v2.0.2

# A tibble: 3 × 3
# Groups:   Species [3]
  Species      min   max
  <fct>      <dbl> <dbl>
1 setosa       4.3   5.8
2 versicolor   7     4.9
3 virginica    4.9   7.9
bvuwiixz

bvuwiixz2#

反馈后编辑

library(PogromcyDanych)
library(tidyverse)
simdb <- PogromcyDanych::serialeIMDB

simdb |>
  group_by(serial) |>
  summarise(
    avg=mean(ocena),
    lnum = min(ocena),
    hnum= max(ocena)) |> left_join(
      simdb |> select(serial,ocena,"Least"=nazwa),
      by=c("serial"="serial",
           "lnum"="ocena")
    ) |> group_by(serial) |> slice_head(n=1) |> left_join(
      simdb |> select(serial,ocena,"Highest"=nazwa),
      by=c("serial"="serial",
           "hnum"="ocena")
    ) |> group_by(serial) |> slice_head(n=1) |> select(-lnum,-hnum)
# A tibble: 198 × 4
# Groups:   serial [198]
   serial                         avg Least                        Highest                      
   <fct>                        <dbl> <fct>                        <fct>                        
 1 Breaking Bad                  8.48 Fly                          Ozymandias                   
 2 Cosmos: A Space-Time Odyssey  9.12 Deeper, Deeper, Deeper Still Unafraid of the Dark         
 3 Planet Earth                  9.23 Mountains                    From Pole to Pole            
 4 Game of Thrones               8.63 The Night Lands              The Rains of Castamere       
 5 True Detective                9.2  The Long Bright Dark         Who Goes There               
 6 The Wire                      8.72 The Target                   The Cost                     
 7 Sherlock                      8.97 The Blind Banker             The Reichenbach Fall         
 8 Cosmos                        8.79 Blues for a Red Planet       One Voice in the Cosmic Fugue
 9 The Sopranos                  8.57 Whitecaps                    Denial, Anger, Acceptance    
10 Leyla ile Mecnun              8.09 Episode #2.25                Pilot

请注意,由于评等是唯一的,因此可能存在重复项;此解决方案只保留第一个条目

8iwquhpp

8iwquhpp3#

请检查以下代码,此处leasthighest变量具有episode name数据,即nazwa
编号

library(PogromcyDanych)
library(tidyverse)

serialeIMDB %>% group_by(serial) %>% mutate(avg=mean(ocena)) %>% arrange(nazwa) %>% 
  filter(ocena ==min(ocena) | ocena ==max(ocena )) %>% 
  mutate(least=case_when(row_number()==1 ~ nazwa), 
  highest= case_when(row_number()==max(row_number()) ~nazwa)) %>% 
  fill(least, highest) %>% slice_tail(n=1) %>% select(serial, avg, least, highest)

创建于2023年1月21日,使用reprex v2.0.2

输出

# A tibble: 198 × 4
# Groups:   serial [198]
   serial                         avg least                         highest                   
   <fct>                        <dbl> <fct>                         <fct>                     
 1 Breaking Bad                  8.48 Fly                           Ozymandias                
 2 Cosmos: A Space-Time Odyssey  9.12 Deeper, Deeper, Deeper Still  Unafraid of the Dark      
 3 Planet Earth                  9.23 From Pole to Pole             Ocean Deep                
 4 Game of Thrones               8.63 The Night Lands               The Rains of Castamere    
 5 True Detective                9.2  The Long Bright Dark          Who Goes There            
 6 The Wire                      8.72 The Target                    -30-                      
 7 Sherlock                      8.97 The Blind Banker              The Reichenbach Fall      
 8 Cosmos                        8.79 One Voice in the Cosmic Fugue Journeys in Space and Time
 9 The Sopranos                  8.57 Denial, Anger, Acceptance     Whitecaps                 
10 Leyla ile Mecnun              8.09 Pilot                         Episode #2.25             
# … with 188 more rows
# ℹ Use `print(n = ...)` to see more rows

相关问题