2017-07-24 22 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

我認爲這絕對會削減我的一些樣本記錄。 –