gpt4 book ai didi

使用 fct_relevel 按组对因子重新排序仅更改第一组中的因子顺序

转载 作者:行者123 更新时间:2023-12-04 07:35:19 25 4
gpt4 key购买 nike

上下文:我需要使用因子顺序来使 arrange 以某种方式显示表格。即我想在每个组的末尾得到一个“总计”行。

问题:使用 fct_relevel 我只在第一组中实现了预期的输出。

重复:

library(dplyr)
library(forcats)

total_masses <- starwars %>%
group_by(species) %>%
summarise(mass = sum(mass),
name = "Total mass")

df <- bind_rows(starwars, total_masses) %>%
select(species, name, mass) %>%
group_by(species) %>%
mutate(name = fct_relevel(name, "Total mass", after = Inf)) %>%
arrange(mass, name, species)
df
#> # A tibble: 125 x 3
#> # Groups: species [38]
#> species name mass
#> <chr> <fct> <dbl>
#> 1 Aleena Ratts Tyerell 15
#> 2 Aleena Total mass 15
#> 3 Yoda's species Total mass 17 # I expect this line to be at the end of the Yoda's species group
#> 4 Yoda's species Yoda 17
#> 5 Ewok Total mass 20
#> 6 Ewok Wicket Systri Warrick 20
#> 7 Droid R2-D2 32
#> 8 Droid R5-D4 32
#> 9 Dug Total mass 40
#> 10 Dug Sebulba 40
#> # ... with 115 more rows

reprex package 创建于 2021-05-31 (v2.0.0)

我希望每个 Total mass 都在每个 species 类别的末尾:

   species        name                   mass
<chr> <fct> <dbl>
1 Aleena Ratts Tyerell 15
2 Aleena Total mass 15
3 Yoda's species Yoda 17
4 Yoda's species Total mass 17
etc...

我觉得解决方案非常明显,但我找不到合适的关键字来解决它...欢迎任何提示!

最佳答案

然后只执行 fct_relevel 而不分组,然后再次分组并在组内做任何你想做的事情。

library(dplyr)
library(forcats)

total_masses <- starwars %>%
group_by(species) %>%
summarise(mass = sum(mass),
name = "Total mass")

# your codes
df <- bind_rows(starwars, total_masses) %>%
select(species, name, mass) %>%
group_by(species) %>%
mutate(name = fct_relevel(name, "Total mass", after = Inf)) %>%
arrange(mass, name, species)
# Which result Total Mass is at 2nd so it will be above other levels
levels(df$name)
#> [1] "Ratts Tyerell" "Total mass" "Dexter Jettster"
#> [4] "Ki-Adi-Mundi" "Mas Amedda" "Zam Wesell"
#> [7] "BB8" "C-3PO" "IG-88"
...

除此之外,您还可以先ungroup,然后是fct_relevel,然后是group_by,然后是arrange

df <- bind_rows(starwars, total_masses) %>% 
select(species, name, mass) %>% ungroup() %>%
mutate(name = fct_relevel(name, "Total mass", after = Inf)) %>%
group_by(species) %>%
arrange(mass, name, species)

# Output
df
#> # A tibble: 125 x 3
#> # Groups: species [38]
#> species name mass
#> <chr> <fct> <dbl>
#> 1 Aleena Ratts Tyerell 15
#> 2 Aleena Total mass 15
#> 3 Yoda's species Yoda 17
#> 4 Yoda's species Total mass 17
#> 5 Ewok Wicket Systri Warrick 20
#> 6 Ewok Total mass 20
#> 7 Droid R2-D2 32
#> 8 Droid R5-D4 32
#> 9 Dug Sebulba 40
#> 10 Dug Total mass 40
#> # … with 115 more rows

# As you see now Total Mass is at the last item
levels(df$name)
#> [1] "Ackbar" "Adi Gallia" "Anakin Skywalker"
#> [4] "Arvel Crynyd" "Ayla Secura" "Bail Prestor Organa"
#> [7] "Barriss Offee" "BB8" "Ben Quadinaros"
#> [10] "Beru Whitesun lars" "Bib Fortuna" "Biggs Darklighter"
#> [13] "Boba Fett" "Bossk" "C-3PO"
#> [16] "Captain Phasma" "Chewbacca" "Cliegg Lars"
#> [19] "Cordé" "Darth Maul" "Darth Vader"
#> [22] "Dexter Jettster" "Dooku" "Dormé"
#> [25] "Dud Bolt" "Eeth Koth" "Finis Valorum"
#> [28] "Finn" "Gasgano" "Greedo"
#> [31] "Gregar Typho" "Grievous" "Han Solo"
#> [34] "IG-88" "Jabba Desilijic Tiure" "Jango Fett"
#> [37] "Jar Jar Binks" "Jek Tono Porkins" "Jocasta Nu"
#> [40] "Ki-Adi-Mundi" "Kit Fisto" "Lama Su"
#> [43] "Lando Calrissian" "Leia Organa" "Lobot"
#> [46] "Luke Skywalker" "Luminara Unduli" "Mace Windu"
#> [49] "Mas Amedda" "Mon Mothma" "Nien Nunb"
#> [52] "Nute Gunray" "Obi-Wan Kenobi" "Owen Lars"
#> [55] "Padmé Amidala" "Palpatine" "Plo Koon"
#> [58] "Poe Dameron" "Poggle the Lesser" "Quarsh Panaka"
#> [61] "Qui-Gon Jinn" "R2-D2" "R4-P17"
#> [64] "R5-D4" "Ratts Tyerell" "Raymus Antilles"
#> [67] "Rey" "Ric Olié" "Roos Tarpals"
#> [70] "Rugor Nass" "Saesee Tiin" "San Hill"
#> [73] "Sebulba" "Shaak Ti" "Shmi Skywalker"
#> [76] "Sly Moore" "Tarfful" "Taun We"
#> [79] "Tion Medon" "Wat Tambor" "Watto"
#> [82] "Wedge Antilles" "Wicket Systri Warrick" "Wilhuff Tarkin"
#> [85] "Yarael Poof" "Yoda" "Zam Wesell"
#> [88] "Total mass"

reprex package 创建于 2021-05-31 (v2.0.0)

关于使用 fct_relevel 按组对因子重新排序仅更改第一组中的因子顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67776841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com