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

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

问题描述



例如:

  m <矩阵(c(1,2,1,3,1,4,1,2,2,3,2,3,1,2,5 ),ncol = 3)
m
duplicateated(m [,1])$ ​​b $ b

输出:

  [,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

但是,我不想要输出。我想要:

  [1] TRUE FALSE TRUE FALSE TRUE 

因为行[1,1]的值在m列1中显示3次。

解决方案

当我看到这个问题时,我问自己Jim Holtman或Bill Dunlap对Rhelp的建议是什么?没有看档案,但我认为他们可能建议使用两个并行应用程序重复的,其中一个默认值,一个与fromLast参数,并结合一个向量OR运算符。

  duplicateated(m [,1])|重复(m [,1],fromLast = TRUE)
[1] TRUE FALSE TRUE FALSE TRUE


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

For example:

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

Outputs:

     [,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

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

解决方案

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天全站免登陆