简体   繁体   English

删除R中数据帧每一行中的绝对重复项

[英]Remove absolute duplicates in each row of a data frame in R

I am trying to remove all the rows that have absolute duplicates in my data frame. 我正在尝试删除数据框中具有绝对重复项的所有行。 Here is an example. 这是一个例子。

library(gtools)
vector <-  c(15.3, -31.8, -35.6, -14.5, 3.1,-24.5)
vector.combo <- data.frame(combinations(n = 12, r = 6, v = c(vector,-vector)))

When you run the above command, you will basically have a data frame containing all of the unique combinations of the elements of "vector" and "-vector". 当您运行上述命令时,基本上将有一个数据框,其中包含“向量”和“-向量”元素的所有唯一组合。 For instance, you will see something like this: 例如,您将看到以下内容:

-35.6   -31.8   -15.3   -3.1    3.1   35.6

Now, I want to remove all the rows that contain the absolute duplicates, meaning I want to remove the rows that has elements such as "35.6" and "-35.6". 现在,我要删除所有包含绝对重复项的行,这意味着我要删除包含诸如“ 35.6”和“ -35.6”之类的元素的行。

I try this: 我尝试这样:

vector.combo[!duplicated(abs(vector.combo)),]

but it did not work. 但它没有用。

Any tip would be appreciated. 任何提示将不胜感激。

Thanks! 谢谢!

I think you want the below: 我认为您需要以下内容:

library(gtools)
vector <-  c(15.3, -31.8, -35.6, -14.5, 3.1,-24.5)
vector.combo <- data.frame(combinations(n = 12, r = 6, v = c(vector,-vector)))

unique_combo <- vector.combo[apply(abs(vector.combo), 1, function(x) length(unique(x))) ==6,]

The index goes row by row over the absolute value of the table, and counts how many unique elements. 索引逐行遍历表的绝对值,并计算有多少个唯一元素。 If the number of unique elements is 6, it returns a true, otherwise a false. 如果唯一元素的数量为6,则返回true,否则返回false。 We then use that as an index to vector.combo . 然后,我们将其用作vector.combo的索引。

Solution using dplyr: 使用dplyr的解决方案:

library(gtools)
library(dplyr)

vector <-  c(15.3, -31.8, -35.6, -14.5, 3.1,-24.5)
vector.combo <- data.frame(combinations(n = 12, r = 6, v = c(vector,-vector)))
dup_idx <- 
  vector.combo %>% 
  transmute_all(abs) %>% 
  duplicated()

vector.combo[!dup_idx,]

Regards Paweł 问候Paweł

get_rid <- c()
for ( i in 1 : length(vector.combo[,1]) ) {
    if ( length(unique(abs(vector.combo[i,]))) != 6 ) {
    get_rid <- c(get_rid, i)
    }
}
vector.combo <- vector.combo[-get_rid,]

That should do it. 那应该做。

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

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