删除数据框中至少有两个重复列的行

标签 r dataframe duplicates

我有以下数据框:

df=data.frame(word1 = c("Hello", "ah", "why"), word2 = c("dress", "ah", "ahi"), 
              word3 = c("english", "ah", "ahi"), x = c(1L,6L, 1L))

> df
  word1 word2   word3 x
1 Hello dress english 1
2    ah    ah      ah 6
3   why   ahi     ahi 1

我想删除至少有两列彼此相等的 word1word2word3 的行。

例如,我们示例中所需的结果是:

> df
  word1 word2   word3 x
1 Hello dress english 1

最佳答案

apply 的几个选项:

cols = c("word1", "word2", "word3")
df[!apply(df[cols], 1, anyDuplicated), ]
#   word1 word2   word3 x
# 1 Hello dress english 1

df[apply(df[cols], 1, function(x) length(unique(x)) == length(x)), ]
#   word1 word2   word3 x
# 1 Hello dress english 1

关于删除数据框中至少有两个重复列的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67092708/

相关文章:

python - sklearn 随机森林分类器可以按树调整样本大小,以处理类别不平衡吗?

r - Emacs ESS 模式 TAB 停止缩进

r - 如何将 openstreetmap 与 R 中的点结合起来

xml - R readHTMLTable 无法加载外部实体

python - 将多索引与多个列级别和数据框合并

c++ - 删除包含重复项的动态指针数组

c++ - 删除 multimap 中的重复键

python - 获取 Pandas 中多个日期时间列的最小值

python - 根据索引将大型数据框中的数据除以较小数据框中的数据

python - SQLAlchemy:分离对象的修改