Jaz*_*Man 9 combinations dataset
所以每种成分都有4种效果 http://www.uesp.net/wiki/Skyrim:Ingredients
如果我结合两种成分.魔药将具有两组相交的奖励效果.我不能两次使用相同的成分.为了生成所有2种成分的可能性,我只列出了成分对的成分列表.我取了列表的头部,并将其与列表中每个元素的其余部分进行比较,每次迭代都删除头部.这避免了欺骗.
我被困了.我不知道如何在没有欺骗的情况下生成3种成分组合.有什么建议?
Zac*_*ach 14
听起来像是每个人最喜欢的编程语言的工作,R!
library(XML)
tables <- readHTMLTable('http://www.uesp.net/wiki/Skyrim:Ingredients',
stringsAsFactors=FALSE)
potions <- tables[[1]]
twoway <- data.frame(t(combn(potions$Name,2)))
threeway <- data.frame(t(combn(potions$Name,3)))
Run Code Online (Sandbox Code Playgroud)
BAM!
> head(twoway)
X1 X2
1 Abecean Longfin Bear Claws
2 Abecean Longfin Bee
3 Abecean Longfin Beehive Husk
4 Abecean Longfin Bleeding Crown
5 Abecean Longfin Blisterwort
6 Abecean Longfin Blue Butterfly Wing
> head(threeway)
X1 X2 X3
1 Abecean Longfin Bear Claws Bee
2 Abecean Longfin Bear Claws Beehive Husk
3 Abecean Longfin Bear Claws Bleeding Crown
4 Abecean Longfin Bear Claws Blisterwort
5 Abecean Longfin Bear Claws Blue Butterfly Wing
6 Abecean Longfin Bear Claws Blue Dartwing
Run Code Online (Sandbox Code Playgroud)
使用该write.csv
命令将表保存为csv文件.
/编辑:解释我在做什么:XML包中包含readHTMLTable函数,它将所有html表从一个网站拉为data.frames并将它们保存为列表.此列表中的第一个表是我们想要的表.所述combn函数找到所有的2路,3路,和n的方式组合药水的名字,并返回结果作为基质.我使用t函数来转置此矩阵,因此每个组合都是一行,然后将其转换为数据帧.这很容易扩展到n种成分的组合.
/编辑2:我写了一个函数来将n路表保存到用户指定的csv文件中.我也对它进行了一些重新设计,因为转置巨大的matricies在计算上是昂贵的.这个版本应该允许你计算4向表,虽然它需要很长时间,我不知道它是否与游戏相关.
nway <- function(n, filepath, data=potions) {
nway <- combn(data$Name, n, simplify = FALSE)
nway <- do.call(rbind,nway)
write.csv(nway,filepath, row.names=FALSE)
}
nway(4,'~/Desktop/4way.csv')
Run Code Online (Sandbox Code Playgroud)
/编辑3:这里有一些代码可以找到实际工作的药水.它效率不高,可能会大大改善:
#Given an ingredient, lookup effects
findEffects <- function(Name) { #Given a name, lookup effects
potions[potions$Name==Name,3:6]
}
#2-way potions
intersectTwoEffects <- function(x) {
Effects1 <- findEffects(x[1])
Effects2 <- findEffects(x[2])
Effects <- unlist(intersect(Effects1,Effects2))
Effects <- c(x[1],x[2],Effects)
length(Effects) <- 6
names(Effects) <- NULL
c(Effects,sum(is.na(Effects)))
}
twoway <- lapply(twoway,intersectTwoEffects)
twoway <- do.call(rbind,twoway)
twoway <- twoway[twoway[,7]<4,-7] #remove combos with no effect
write.csv(twoway,'~/Desktop/twoway.csv',row.names=FALSE)
#3-way potions
intersectThreeEffects <- function(x) {
Effects1 <- findEffects(x[1])
Effects2 <- findEffects(x[2])
Effects3 <- findEffects(x[3])
Effects <- c(intersect(Effects1,Effects2),intersect(Effects1,Effects3),intersect(Effects2,Effects3))
Effects <- unlist(unique(Effects))
Effects <- c(x[1],x[2],x[3],Effects)
length(Effects) <- 8
names(Effects) <- NULL
c(Effects,sum(is.na(Effects)))
}
threeway <- lapply(threeway,intersectThreeEffects)
threeway <- do.call(rbind,threeway)
threeway <- threeway[threeway[,9]<5,-9] #remove combos with no effect
write.csv(threeway,'~/Desktop/threeway.csv',row.names=FALSE)
Run Code Online (Sandbox Code Playgroud)