识别重复并标记第一次出现和所有其他 [英] Identify duplicates and mark first occurrence and all others

查看:9
本文介绍了识别重复并标记第一次出现和所有其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试识别在矩阵中表示两次或多次的所有行.

I'm trying to identify all rows which are represented twice or more in a matrix.

例如:

m <- matrix(c(1,2,1,3,1,4,1,2,2,3,2,3,1,2,5), ncol = 3)
m
duplicated(m[,1])

输出:

     [,1] [,2] [,3]
[1,]    1    4    2
[2,]    2    1    3
[3,]    1    2    1
[4,]    3    2    2
[5,]    1    3    5

[1] FALSE FALSE  TRUE FALSE  TRUE

但是,我不想要那个输出.我要:

However, I do not want that output. I want:

[1] TRUE FALSE TRUE FALSE TRUE

因为row[1,1]的值在m的第1列出现了3次.

since row[1,1]'s value appears 3 times in m's column 1.

推荐答案

当我看到这个问题时,我问自己Jim Holtman 或 Bill Dunlap 对 Rhelp 有何建议?".没有查看档案,但我认为他们可能建议使用两个并行".duplicated 的应用,一种使用默认值,一种使用 fromLast 参数并与向量 OR (|) 运算符结合.

When I saw this question I asked myself "what would Jim Holtman or Bill Dunlap advise on Rhelp?". Haven't looked in the archives, but I think they might have advised using two "parallel" applications of duplicated, one with the defaults and one with the fromLast parameter and conjoining with a vector OR (|) operator.

duplicated(m[,1]) | duplicated(m[,1], fromLast=TRUE)
[1]  TRUE FALSE  TRUE FALSE  TRUE

这篇关于识别重复并标记第一次出现和所有其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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