且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有更优雅的方式来找到重复的记录吗?

更新时间:2023-10-23 10:59:04

重复(df )将给您一个逻辑向量(所有值由T / F组成),然后您可以将其用作数据帧的索引。

duplicated(df) will give you a logical vector (all values consisting of either T/F), which you can then use as an index to your dataframe rows.

# indx will contain TRUE values wherever in df$var there is a duplicate
indx <- duplicated(df$var)
df[indx, ]  #note the comma 






你可以一起把它放在一起


You can put it all together in one line

df[duplicated(df$var), ]  # again, the comma, to indicate we are selected rows