Apologies if the question is a bit wordy, however I am sure an example here will clear things up. I have the following dataframe:
structure(list(teamName = c("Brazil", "Germany", "Spain", "England",
"France", "Spain", "France", "Germany", "Brazil", "England",
"Spain", "France", "Brazil"), wins = c(0, 0, 0, 0, 0, 1, 1, 1,
1, 1, 1, 2, 1), losses = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1), ties = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0)), .Names = c("teamName",
"wins", "losses", "ties"), row.names = c(NA, 13L), class = "data.frame")
teamName wins losses ties
1 Brazil 0 0 0
2 Germany 0 0 0
3 Spain 0 0 0
4 England 0 0 0
5 France 0 0 0
6 Spain 1 0 0
7 France 1 0 0
8 Germany 1 0 0
9 Brazil 1 0 0
10 England 1 0 0
11 Spain 1 0 1
12 France 1 0 1
13 Brazil 1 1 0
which has some soccer countries, and I would like to filter this dataframe such that it only includes the last row for each team. Although there are 5 teams here, it is not necessarily the case that the 5 last rows in the dataframe are the 5 rows I want. In this case, there are 2 rows for Brazil before the last Germany row.
For this example, the row indices for the last row for each team are 8, 10, 11, 12, and 13.
Is there an easy way to get these indices without having to use a for loop?
Thanks!
You can do this in base R using duplicated
Soccer[!duplicated(Soccer$teamName, fromLast=TRUE),]
teamName wins losses ties
8 Germany 1 0 0
10 England 1 0 0
11 Spain 1 0 1
12 France 2 0 1
13 Brazil 1 1 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With