Reputation:
I have the follwing data frame which happens to be NBA draft data:
draft_year draft_round teamid playerid draft_from
1961 1 Bos Pol1 Nan
2001 1 LA Ben2 Cal
1967 2 Min Mac2 Nan
2001 1 LA Ben2 Cal
2000 1 C Sio1 Bud
2000 1 C Gio1 Bud
I would like to find & remove only those rows with duplicates in playerid. For obvious reasons, the remaining duplicates have a meaningful purpose and must be kept.
Upvotes: 3
Views: 166
Reputation: 887851
You could also use dplyr
library(dplyr)
unique(df, group_by="playerid")
# draft_year draft_round teamid playerid draft_from
#1 1961 1 Bos Pol1 Nan
#2 2001 1 LA Ben2 Cal
#3 1967 2 Min Mac2 Nan
#5 2000 1 C Sio1 Bud
#6 2000 1 C Gio1 Bud
Or
df %>%
group_by(playerid) %>%
filter(row_number()==1)
Upvotes: 2
Reputation: 5837
You can achieve this by using duplicated
or unique()
new_df <- df[!duplicated( df$playerid), ]
Upvotes: 2
Reputation: 92300
In data.table
package you have a by
parameter in the unique
function
library(data.table)
unique(setDT(df), by = "playerid")
# draft_year draft_round teamid playerid draft_from
# 1: 1961 1 Bos Pol1 Nan
# 2: 2001 1 LA Ben2 Cal
# 3: 1967 2 Min Mac2 Nan
# 4: 2000 1 C Sio1 Bud
# 5: 2000 1 C Gio1 Bud
Upvotes: 2