我想知道是否有任何R套件可以帮助我获得所有可能的融合组合。 换句话说,我有一组(1,2,3,4,5,6,7,8,9,10),我想得到一组5个子集,例如(1,2,3) ,4,5,6),(7),(8),(9),(10)或(2,3,4,5,6),(1,7),(8),(9), (10)或(3,4,6)(1,5)(2,7)(9,10)(8)......等 我设法编写了一个示例测试代码,但速度非常慢:
library(partitions)
set = seq(1,10)
possibleCombinations = restrictedparts(10,5,include.zero = F)
fusionCombinations(set,possibleCombinations[,1],1,list())
fusionCombinations= function(set,setSize,i,prefix){
o=combn(predicted,setSize[i])
for(t in seq(1,ncol(o))){
prefixtemp = prefix
remaining = setdiff(predicted,o[,t])
if(i+1<=length(setSize)){
if(setSize[i+1]>1){
prefixtemp=append(prefixtemp,list(o[,t]))
result=append(result,fusionCombinations(remaining,setSize,i+1,prefixtemp))
}else{
prefixtemp=prefix
prefixtemp=append(prefixtemp,list(o[,t]))
prefixtemp=apply(t(remaining),1,append,prefixtemp)
result=append(result,list(prefixtemp))
}
}
}
return(result)
}
有什么想法吗?
答案 0 :(得分:1)
此函数将矢量和样本数作为输入,然后找到nSets-1随机剪切(将最后一个剪切到矢量的最后一个元素),然后依次打印矢量的子集。
subsets <- function(vector, nSets) {
cuts <- sample(1:(length(vector)-1), nSets-1, replace = F)
cuts <- c(0,sort(cuts),length(vector))
for (i in 1:nSets) {
print(vector[(cuts[i]+1):cuts[i+1]])
}
}
它也可以与非数字向量一起使用。
答案 1 :(得分:1)
我的想法是使用method4
和<html>
<body>
<?php
$server="localhost";
$username="root";
$password="";
$connect_mysql=mysql_connect($server,$username,$password) or die ("Connection Failed!");
$mysql_db=mysql_select_db("wordpress",$connect_mysql) or die ("Could not Connect to Database");
$query = "SELECT * FROM category ";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
$i=0;
while($rows=mysql_fetch_array($result))
{
$roll[$i]=$rows['name_category'];
$i++;
}
$total_elmt=count($roll);
?>
<form method="POST" action="">
Select cupcake_category : <select name="sel">
<option>Select</option>
<?php
for($j=0;$j<$total_elmt;$j++)
{
?><option><?php
echo $roll[$j];
?></option><?php
}
?>
</select>
<input name="submit" type="submit" value="Search"/><br />
</form>
<?php
if(isset($_POST['submit']))
{
$value=$_POST['sel'];
$query2 = "SELECT * FROM cupcakes, category, taste WHERE
cupcakes.cupcake_id = category.id_category AND
category.id_category = taste.id_taste AND
category.cupcake_id = taste.cupcake_id";
$result2=mysql_query($query2) or die("Query Failed : ".mysql_error());
while($row=mysql_fetch_array($result2))
{
echo "cupcake name: ".$row['cupcake_name']."<br/>";
echo "price: ".$row['cupcake_price']."<br/>";
}
mysql_close($connect_mysql);
}
?>
:
setparts
示例:
split
library(partitions)
f <- function(n,m)
{
apply( setparts(restrictedparts(n,m,include.zero=FALSE)),
2,
function(k){split(1:n,k)})
}
函数> X <- f(4,3)
> X
[[1]]
[[1]]$`1`
[1] 1 4
[[1]]$`2`
[1] 2
[[1]]$`3`
[1] 3
[[2]]
[[2]]$`1`
[1] 1 2
[[2]]$`2`
[1] 3
[[2]]$`3`
[1] 4
[[3]]
[[3]]$`1`
[1] 1 3
[[3]]$`2`
[1] 2
[[3]]$`3`
[1] 4
[[4]]
[[4]]$`1`
[1] 2 4
[[4]]$`2`
[1] 1
[[4]]$`3`
[1] 3
[[5]]
[[5]]$`1`
[1] 2 3
[[5]]$`2`
[1] 1
[[5]]$`3`
[1] 4
[[6]]
[[6]]$`1`
[1] 3 4
[[6]]$`2`
[1] 1
[[6]]$`3`
[1] 2
> for ( i in 1:length(X) ) { prettyPrint(X[[i]]) }
[1] "(1, 4)( 2 )( 3 )"
[1] "( 1:2 )( 3 )( 4 )"
[1] "(1, 3)( 2 )( 4 )"
[1] "(2, 4)( 1 )( 3 )"
[1] "( 2:3 )( 1 )( 4 )"
[1] "( 3:4 )( 1 )( 2 )"
>
:
> X <- f(5,2)
> X
[[1]]
[[1]]$`1`
[1] 1 2 4 5
[[1]]$`2`
[1] 3
[[2]]
[[2]]$`1`
[1] 1 2 3 5
[[2]]$`2`
[1] 4
[[3]]
[[3]]$`1`
[1] 1 2 3 4
[[3]]$`2`
[1] 5
[[4]]
[[4]]$`1`
[1] 1 3 4 5
[[4]]$`2`
[1] 2
[[5]]
[[5]]$`1`
[1] 2 3 4 5
[[5]]$`2`
[1] 1
[[6]]
[[6]]$`1`
[1] 1 2 5
[[6]]$`2`
[1] 3 4
[[7]]
[[7]]$`1`
[1] 1 2 4
[[7]]$`2`
[1] 3 5
[[8]]
[[8]]$`1`
[1] 1 2 3
[[8]]$`2`
[1] 4 5
[[9]]
[[9]]$`1`
[1] 1 3 5
[[9]]$`2`
[1] 2 4
[[10]]
[[10]]$`1`
[1] 1 3 4
[[10]]$`2`
[1] 2 5
[[11]]
[[11]]$`1`
[1] 1 4 5
[[11]]$`2`
[1] 2 3
[[12]]
[[12]]$`1`
[1] 2 3 5
[[12]]$`2`
[1] 1 4
[[13]]
[[13]]$`1`
[1] 2 3 4
[[13]]$`2`
[1] 1 5
[[14]]
[[14]]$`1`
[1] 2 4 5
[[14]]$`2`
[1] 1 3
[[15]]
[[15]]$`1`
[1] 3 4 5
[[15]]$`2`
[1] 1 2
> for ( i in 1:length(X) ) { prettyPrint(X[[i]]) }
[1] "(1, 2, 4, 5)( 3 )"
[1] "(1, 2, 3, 5)( 4 )"
[1] "( 1:4 )( 5 )"
[1] "(1, 3, 4, 5)( 2 )"
[1] "( 2:5 )( 1 )"
[1] "(1, 2, 5)( 3:4 )"
[1] "(1, 2, 4)(3, 5)"
[1] "( 1:3 )( 4:5 )"
[1] "(1, 3, 5)(2, 4)"
[1] "(1, 3, 4)(2, 5)"
[1] "(1, 4, 5)( 2:3 )"
[1] "(2, 3, 5)(1, 4)"
[1] "( 2:4 )(1, 5)"
[1] "(2, 4, 5)(1, 3)"
[1] "( 3:5 )( 1:2 )"
>
答案 2 :(得分:0)
此功能基于Petr Savicky于2012年7月20日发布给R-help [link]的方法。
allcombs <- function(num, from=0, to=num) {
m <- as.matrix(expand.grid(rep(list(0:1), times = num)))
n.items <- rowSums(m)
m[n.items >= from & n.items <= to, ]
}
在您的示例中,您将使用语句
allcombs(10, from=1, to=5)
获取大小为1到5的所有637个可能子集。返回的值是一个矩阵,其中的行对应于每个可能的组合,而列对应于项目编号。