Ho un frame di dati DAT1risultato Combine da top_n con una categoria "Altro" in dplyr
Country Count
1 AUS 1
2 NZ 2
3 NZ 1
4 USA 3
5 AUS 1
6 IND 2
7 AUS 4
8 USA 2
9 JPN 5
10 CN 2
Prima di tutto voglio riassumere "Count" per "Country". Poi i primi 3 conteggi totali per paese dovrebbero essere combinati con una riga aggiuntiva "Altri", che è la somma dei paesi che non fanno parte della top 3.
Il risultato atteso, pertanto potrebbe essere:
Country Count
1 AUS 6
2 JPN 5
3 USA 5
4 Others 7
Ho provato il codice seguente, ma non sono riuscito a capire come posizionare la riga "Altri".
dat1 %>%
group_by(Country) %>%
summarise(Count = sum(Count)) %>%
arrange(desc(Count)) %>%
top_n(3)
Questo codice dà attualmente:
Country Count
1 AUS 6
2 JPN 5
3 USA 5
Qualsiasi aiuto sarebbe molto apprezzato.
dat1 <- structure(list(Country = structure(c(1L, 5L, 5L, 6L, 1L, 3L,
1L, 6L, 4L, 2L), .Label = c("AUS", "CN", "IND", "JPN", "NZ",
"USA"), class = "factor"), Count = c(1L, 2L, 1L, 3L, 1L, 2L,
4L, 2L, 5L, 2L)), .Names = c("Country", "Count"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"))
associati Q & A: [Creazione di un “altro” campo] (http://stackoverflow.com/questions/23730067/creating-an-other-field]). – Henrik