根据另一个变量选择因子的前十个级别

时间:2014-01-30 14:47:50

标签: r

所以我读过有关数据文件的信息,但没有找到我真正需要的内容。

我有不同年份约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")

2 个答案:

答案 0 :(得分:4)

以下是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的未来版本可能具有以下内容:

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

跟踪此功能的进度

答案 1 :(得分:2)

调用您的数据x。以下是一种使用前两个SP值提取IA值的方法:

使用SP找出每个aggregate值的最大值:

 (maxIa <- aggregate(IA ~ SP, data=x, FUN=max))
##                                            SP         IA
## 1             Aglais urticae (Linnaeus, 1758)  424.20166
## 2 Anthocharis cardamines (Linnaeus,     1758)   86.72272
## 3               Apatura iris (Linnaeus, 1758)   56.66667
## 4      Aphantopus hyperantus (Linnaeus, 1758) 5693.16662
## 5            Aporia crataegi (Linnaeus, 1758)  332.63158

IA排序:

##     (maxIa <- maxIa[order(maxIa$IA, decreasing=TRUE),])
##                                            SP         IA
## 4      Aphantopus hyperantus (Linnaeus, 1758) 5693.16662
## 1             Aglais urticae (Linnaeus, 1758)  424.20166
## 5            Aporia crataegi (Linnaeus, 1758)  332.63158
## 2 Anthocharis cardamines (Linnaeus,     1758)   86.72272
## 3               Apatura iris (Linnaeus, 1758)   56.66667

提取前2个SP值:

N <- 2
(top <- maxIa[seq(N), 'SP', drop=FALSE])
##                                       SP
## 4 Aphantopus hyperantus (Linnaeus, 1758)
## 1        Aglais urticae (Linnaeus, 1758)

result <- merge(x, top, by='SP')
head(result)
##                                SP YEAR        IA
## 1 Aglais urticae (Linnaeus, 1758) 2003 115.91579
## 2 Aglais urticae (Linnaeus, 1758) 2000  74.66667
## 3 Aglais urticae (Linnaeus, 1758) 1994 424.20166
## 4 Aglais urticae (Linnaeus, 1758) 1996  49.96923
## 5 Aglais urticae (Linnaeus, 1758) 1997 274.38291
## 6 Aglais urticae (Linnaeus, 1758) 2006  36.84651