简体   繁体   English

如何在 R 的矩阵列中不唯一的 select 行

[英]How to select rows which are not unique in columns of matrix in R

I have a big data set but i can explain through a simple example.我有一个大数据集,但我可以通过一个简单的例子来解释。 For example i have a matrix "x"例如我有一个矩阵“x”

x<- matrix(c(3,3,3,4,3,3,5,5,5), nrow=3, byrow=T)

now i need second row in which "x" is not unique entries.现在我需要第二行,其中“x”不是唯一的条目。 First and third rows are equal in the sense of columns.第一行和第三行在列的意义上是相等的。

Regards and thanks in advance,提前致以问候和感谢,

Iftikhar Ahmad伊夫蒂哈尔·艾哈迈德

You could also take advantage of the fact that equality means the standard deviation will always be 0. unfortunately we have to convert these 0s to logical either with a logical expression (below) or with as.logical .您还可以利用相等意味着标准偏差始终为 0 的事实。不幸的是,我们必须使用逻辑表达式(如下)或使用as.logical将这些 0 转换为逻辑。

x[apply(x, 1, sd) > 0, ]

Update更新

Did some benchmarking of @joran and my solutions.对@joran 和我的解决方案进行了一些基准测试。 Mine lost:(我的丢了:(

x <- matrix(sample(3:5,30000,T), ncol=3)    

system.time(x2 <- x[apply(x,1,sd) > 0, ])
user  system elapsed 
0.960   0.000   0.961

system.time(x2 <- x[apply(x,1,FUN=function(r){return(length(unique(r)))}) > 1,])
user  system elapsed 
0.470   0.000   0.465

But...但...

If we do a fully vectorised version with a similar theme, we can blow both out of the water如果我们做一个具有相似主题的完全矢量化版本,我们可以将两者从水中吹走

system.time(x2 <- x[rowSums(abs(x - rowMeans(x))) != 0, ])
user  system elapsed 
0.000   0.000   0.001

Is this what you're looking for:这是你要找的:

x[apply(x,1,FUN=function(r){return(length(unique(r)))}) > 1,]

that will select rows with more than one unique value in them.这将 select 行中包含多个唯一值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM