简体   繁体   English

与R中的大数据集匹配

[英]Matching with large data set in R

I received this problem from a professor of mine to solve in R. Here's what I came up with: 我从我的一位教授那里得到了这个问题来解决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

What I think the code is doing: 我认为代码在做什么:

  1. Sampling 5 of the buttons on the machine without replacement. 在没有更换的情况下对机器上的5个按钮进行采样。
  2. Plugging those 5 numbers in the A*B+C-D+E formula. 在A * B + C-D + E公式中插入这5个数字。
  3. Spitting out a final answer. 吐出最后的答案。 Repeat 80,000 times. 重复80,000次。
  4. Last command should check all the outputs from the formula against the snack numbers in the machine and return a Boolean. 最后一个命令应该根据机器中的零食号检查公式中的所有输出并返回一个布尔值。

However, the Boolean comes back with 25 Trues when there should be 1 false. 但是,当应该有1个假时,布尔值会返回25个真值。 Where did I go wrong? 我哪里做错了?

I don't know why you try to solve this with random sampling. 我不知道你为什么试图用随机抽样解决这个问题。 You could never trust any answer other than that you can get all snacks. 你永远不会相信任何答案,除了你可以得到所有的零食。 I'd use this: 我用这个:

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))]

However, this doesn't give me a snack I can't get either. 然而,这并没有给我一个我无法得到的零食。 Maybe I'm misinterpreting the wording. 也许我误解了措辞。

For starters remove the for loop. 对于初学者,删除for循环。 It is very inefficient: 这是非常低效的:

random <- matrix(replicate(8e4, sample(buttons,5,replace=F)), ncol=5, byrow=TRUE)
combos <-random[,1]*random[,2]+random[,3]-random[,4]+random[,5]

When I expand to one million combos, it matches all TRUE each time. 当我扩展到一百万个组合时,它每次都匹配所有TRUE。 There may be some confusion I'm not seeing in the question. 在问题中我可能没有看到一些混乱。 Or your professor is a very cruel person. 或者你的教授是一个非常残酷的人。

Update: 更新:

I changed the buttons vector as mentioned in the comments: 我更改了注释中提到的按钮向量:

solutions[!solutions %in% combos]
[1] 2437

Thanks, I can move on with my regularly scheduled life. 谢谢,我可以继续我的定期生活。 Sampling isn't the best way, but problem solved. 采样不是最好的方法,但问题解决了。

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

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