Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...