R语言 将最小值和最大值更改为电影名称

cedebl8k  于 2023-02-10  发布在  其他
关注(0)|答案(3)|浏览(145)

我想做一个表格,列出最不受欢迎和最受欢迎的电影。但是我不知道怎么做。我试着找到一个解决方案,但是我失败了。基本上它应该是这样的:
| 识别号|最不受欢迎的电影|最受欢迎电影|
| - ------|- ------|- ------|
| 1个|某某|蒂耶|
| 第二章|中兴通讯|奎兹|
这是我试过的方法

movies%>%
    group_by(ID) %>%
    summarise(
      Least = min(popularity),
      Most= max(popularity))
vyswwuz2

vyswwuz21#

由于您没有提供实际数据来测试代码,因此我在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
bvn4nwqk

bvn4nwqk2#

反馈后编辑

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

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

4si2a6ki

4si2a6ki3#

请检查以下代码,此处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

相关问题