[英]Matching with large data set in R
我從我的一位教授那里得到了這個問題來解決R.這就是我想出的:
buttons <- c(16,23,61,7,7,7,13,13,13,19,19,21,27,56,56,73,77,87,11,37,41)
combos<- NULL
for(bb in 1:80000){
random<-sample(buttons,5,replace=FALSE)
#A*B+C-D+E
combos[bb]<-(random[1])*(random[2])+(random[3])-(random[4])+(random[5])
}
solutions<-c(917,134,1569,1649,1431,1622,233,2094,1072,915,
1922,2437,2714,2491,1886,2812,426,1673,94,2139,2569,496,2249,1553,1580)
solutions %in% combos
我認為代碼在做什么:
但是,當應該有1個假時,布爾值會返回25個真值。 我哪里做錯了?
我不知道你為什么試圖用隨機抽樣解決這個問題。 你永遠不會相信任何答案,除了你可以得到所有的零食。 我用這個:
buttons <- as.integer(buttons)
solutions <- as.integer(solutions)
#create all combinations of 5 buttons
combos <- t(combn(buttons, 5))
library(combinat)
#permute the combinations
tmp <- lapply(permn(1:5), function(i, solutions, combos) {
#which solutions can be derived from the permuted combination?
solutions[solutions %in% (combos[,i[1]] *
combos[,i[2]] +
combos[,i[3]] -
combos[,i[4]] +
combos[,i[5]])]
}, solutions = solutions, combos = combos)
#which solution can not be achieved?
solutions[!(solutions %in% unlist(tmp))]
然而,這並沒有給我一個我無法得到的零食。 也許我誤解了措辭。
對於初學者,刪除for循環。 這是非常低效的:
random <- matrix(replicate(8e4, sample(buttons,5,replace=F)), ncol=5, byrow=TRUE)
combos <-random[,1]*random[,2]+random[,3]-random[,4]+random[,5]
當我擴展到一百萬個組合時,它每次都匹配所有TRUE。 在問題中我可能沒有看到一些混亂。 或者你的教授是一個非常殘酷的人。
更新:
我更改了注釋中提到的按鈕向量:
solutions[!solutions %in% combos]
[1] 2437
謝謝,我可以繼續我的定期生活。 采樣不是最好的方法,但問題解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.