2017-07-24 47 views
2

蒸馏到功能术语:识别行签名来满足问题的唯一性

给定的值的列表(让我们使用字母)

uniqueList <- letters[1:9] 

和值从该列表中有4列中的数据帧随意组合,其中一些重复的(使用combn这里产生我的数据的合理的传真,但我的数据来自用户输入的数据)

data <- t(combn(uniqueList,4)) 

如何我会识别并选择在数据集中行(及其索引)的最小数目,使得每个值从列表中出现至少一次,不管它出现在列的?

在实际的问题,我试图解决,我需要生成的样本记录的最小数,从实际数据,我必须从值列表中4列至少一次独特的价值。

回答

0

也许这个作品

#Split `data` into rows and then convert to vector 
temp = do.call(c, lapply(1:NROW(data), function(i) data[i,])) 

#Advance along `temp` and see if `unique` gives all elements of uniqueList 
#Extract the first index where that happens 
ind = which(sapply(1:length(temp), function(i) identical(unique(temp[1:i]), uniqueList)))[1] 

#Divide the ind by number of column to obtain row number 
myrow = ceiling(ind/NCOL(data)) 
myrow 
#[1] 6 

identical(sort(unique(as.vector(data[1:myrow,]))), uniqueList) 
#[1] TRUE 
+0

我认为这绝对会削减我的一些样本记录。 –