Select top ten levels of a factor based on another variable

So i have read about data filetering but didnt found what i exactly need.

I have Index values calculated for about 70 species at different years. I have to put the evolution of that index in a graph, over years of observation, but 70 species is pretty too much. I would like to do that graph for the top ten species. Is there an easy way to select these ?

Thanks for help!

Here is a part of my dataset.

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")

Answers


Call your data x. Here's a way to extract SP values with the top two IA values for any year:

Find the max of each SP value with 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

Order by 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

Extract the top 2 SP values:

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

Here are two approaches in 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")

This seems to be a pretty common operation, so a future version of dplyr might have something like the following:

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)

(you can track progress on this feature at https://github.com/hadley/dplyr/issues/229)


Need Your Help

Can I uniquely identify 2 check boxes so that I can add a different image to each?

html css asp.net checkbox

Currently I am using an image for checked and unchecked checkboxes the html and css is below.

How can Azure maintain APPLICATION state?

azure state

I know how Azure preserves SESSION state -- I've implemented it in my app using AppFabric based on Neil MacKenzie's Microsoft Windows Azure Development Cookbook. However, that approach based on