gpt4 book ai didi

r - 根据另一个变量选择一个因素的前十个水平

转载 作者:行者123 更新时间:2023-12-04 12:21:18 26 4
gpt4 key购买 nike

所以我已经阅读了有关数据过滤的内容,但没有找到我真正需要的内容。

我计算了大约 70 个物种在不同年份的指数值。经过多年的观察,我必须将该指数的演变情况用图表表示出来,但 70 个物种太多了。我想为排名前十的物种绘制图表。有没有简单的方法来选择这些?

感谢您的帮助!

这是我的数据集的一部分。

dput(head(AGGIA, n=50))
structure(list(YEAR = structure(c(1L, 2L, 3L, 4L, 6L, 9L, 10L,
12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L,
14L, 16L, 17L, 2L, 4L, 12L, 17L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 1L, 2L, 3L, 4L,
5L), .Label = c("1994", "1995", "1996", "1997", "1998", "2000",
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",
"2009", "2010", "2011"), class = "factor"), SP = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
5L), .Label = c("Aglais urticae (Linnaeus, 1758)", "Anthocharis cardamines (Linnaeus, 1758)",
"Apatura iris (Linnaeus, 1758)", "Aphantopus hyperantus (Linnaeus, 1758)",
"Aporia crataegi (Linnaeus, 1758)", "Araschnia levana (Linnaeus, 1758)",
"Argynnis aglaja (Linnaeus, 1758)", "Argynnis paphia (Linnaeus, 1758)",
"Boloria dia (Linnaeus, 1767)", "Boloria euphrosyne (Linnaeus, 1758)",
"Boloria selene (Denis & Schiffermüller, 1775)", "Brenthis daphne (Bergsträsser, 1780)",
"Brintesia circe (Fabricius, 1775)", "Callophrys rubi (Linnaeus, 1758)",
"Carterocephalus palaemon (Pallas, 1771)", "Celastrina argiolus (Linnaeus, 1758)",
"Clossiana dia (Linnaeus, 1767)", "Clossiana selene (Denis & Schiffermüller, 1775)",
"Coenonympha pamphilus (Linnaeus, 1758)", "Colias croceus (Fourcroy, 1785)",
"Colias hyale (Linnaeus, 1758)", "Colias PC (hyale / alfacariensis) #complexe",
"Cupido argiades (Pallas, 1771)", "Erebia meolans (de Prunner, 1798)",
"Erynnis tages (Linnaeus, 1758)", "Euchloe PC (ausonia / simplonia) #complexe",
"Euphydryas aurinia (Rottemburg, 1775)", "Everes argiades (Pallas, 1771)",
"Gonepteryx rhamni (Linnaeus, 1758)", "Hesperia comma (Linnaeus, 1758)",
"Heteropterus morpheus (Pallas, 1771)", "Hipparchia fagi (Scopoli, 1763)",
"Inachis io (Linnaeus, 1758)", "Iphiclides podalirius (Linnaeus, 1758)",
"Issoria lathonia (Linnaeus, 1758)", "Ladoga camilla (Linnaeus, 1764)",
"Lampides boeticus (Linnaeus, 1767)", "Lasiommata megera (Linnaeus, 1767)",
"Limenitis camilla (Linnaeus, 1764)", "Limenitis reducta Staudinger, 1901",
"Lycaena phlaeas (Linnaeus, 1761)", "Lycaena tityrus (Poda, 1761)",
"Maniola jurtina (Linnaeus, 1758)", "Melanargia galathea (Linnaeus, 1758)",
"Melitaea cinxia (Linnaeus, 1758)", "Melitaea diamina (Lang, 1789)",
"Melitaea didyma (Esper, 1778)", "Melitaea phoebe (Denis & Schiffermüller, 1775)",
"Mellicta athalia (Rottemburg, 1775)", "Mellicta C (athalia / deione / parthenoides) #complexe",
"Mellicta parthenoides (Keferstein, 1851)", "Mesoacidalia aglaja (Linnaeus, 1758)",
"Nymphalis antiopa (Linnaeus, 1758)", "Nymphalis polychloros (Linnaeus, 1758)",
"Ochlodes venatus (Bremer & Grey, 1853)", "Ochlodes venatus faunus (Turati, 1905)",
"Papilio machaon Linnaeus, 1758", "Pararge aegeria (Linnaeus, 1758)",
"Pieris 2 (rapae / mannii / napi) #complexe", "Pieris brassicae (Linnaeus, 1758)",
"Pieris napi (Linnaeus, 1758)", "Pieris PC (rapae / mannii) #complexe",
"Plebeius agestis (Denis & Schiffermüller, 1775)", "Plebejus argus (Linnaeus, 1758)",
"Polygonia c-album (Linnaeus, 1758)", "Polyommatus icarus (Rottemburg, 1775)",
"Polyommatus semiargus (Rottemburg, 1775)", "Pseudophilotes baton (Bergsträsser, 1779)",
"Pyrgus 1 C (malvae / malvoides) #complexe", "Pyronia tithonus (Linnaeus, 1767)",
"Quercusia quercus (Linnaeus, 1758)", "Thymelicus lineola (Ochsenheimer, 1808)",
"Thymelicus sylvestris (Poda, 1761)", "Vanessa atalanta (Linnaeus, 1758)",
"Vanessa cardui (Linnaeus, 1758)"), class = "factor"), IA = c(424.201664417454,
327.83961352657, 49.9692307692308, 274.382905982906, 74.6666666666667,
115.915789473684, 9.33333333333333, 36.846511627907, 20.8, 26.25,
27.5, 41.25, 66.3586956521739, 6.08695652173913, 29.75, 86.7227191413238,
17.5, 40.3388888888889, 42.8882385730212, 70, 38.8888888888889,
8.75, 38.0626895854398, 4.375, 22, 39.2, 56.6666666666667, 35.2173913043478,
1857.06137608157, 1518.9491476416, 2147.58342068648, 3719.62920885931,
1810.39910813824, 2381.12333836848, 718.429409923918, 1538.72862360914,
1264.28700725979, 1915.26299396829, 2194.05837839719, 5693.16661595135,
1267.96203583549, 1400.09190519921, 1948.16102487234, 1991.84853522577,
1490.51593769658, 109.583333333333, 219.128019323671, 233.165217391304,
332.631578947368, 113.618421052632)), .Names = c("YEAR", "SP",
"IA"), row.names = c(NA, 50L), class = "data.frame")

最佳答案

dplyr 中有两种方法

library(dplyr)

# Preserves original order, respects ties
top2 <- AGGIA %.%
group_by(SP) %.%
summarise(IA = max(IA)) %.%
filter(row_number(IA) <= 2)

# Ordered by max(IA), ignores ties
top2 <- AGGIA %.%
group_by(SP) %.%
summarise(IA = max(IA)) %.%
arrange(desc(IA)) %.%
head(2)

# Keep rows of AGGIA that match rows in top2
semi_join(AGGIA, top2, by = "SP")

这似乎是一个很常见的操作,所以 dplyr 的 future 版本可能会像下面这样:

top_n <- function(tbl, by, n = 10) {
by <- substitute(by)

summarise_call <- bquote(summarise(tbl, .z = .(by)))
eval(summarise_call) %.% filter(row_number(.z) <= n)
}

top2 <- AGGIA %.% group_by(SP) %.% top_n(max(IA), n = 2)

(您可以在 https://github.com/hadley/dplyr/issues/229 跟踪此功能的进度)

关于r - 根据另一个变量选择一个因素的前十个水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21459617/

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