Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

r - How to find & remove duplicates in data frames?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...