如何在R的每一行中查找重复项 [英] How to find duplicates within each row in R

查看:82
本文介绍了如何在R的每一行中查找重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找使用R连续重复的重复项?

How do you find if there are duplicates within a row using R?

附件是主管报告层次结构的示例.由于每个人都无法向层次结构中的某人报告,因此一行中不应有重复的数字.

The attached file is an example of a supervisor reporting hierarchy. There should be no repeat numbers within a row, as each person cannot report back to someone down the hierarchy.

在示例图像中,第2行有两个6,第3行有两个10,因此我想创建一个"duplicate"列,其中"Y"表示重复项.欢迎其他任何建议.

In the example image, row 2 has two 6's, row 3 has two 10's, so I want to create a 'duplicate' column indicating "Y" for duplicates. Any other suggestions are welcome.

"for循环(对于1:nrow中的i)"或应用"是更好的选择吗?

is a 'for loop (for i in 1:nrow)' or 'apply' a better option?

推荐答案

您可以使用 ifelse()

data$duplicate <- ifelse(apply(data, 1, function(row) any(duplicated(row))), "Y", "N")

这篇关于如何在R的每一行中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆