Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the tail indices for several values at once in an R dataframe

Tags:

r

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!

like image 922
Canovice Avatar asked Jan 02 '23 06:01

Canovice


1 Answers

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
like image 125
G5W Avatar answered Jan 19 '23 00:01

G5W