Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R找到包含所有值的所有可能组合的最佳集合_R_Select_Optimization_Combinations_Nonlinear Optimization - Fatal编程技术网

R找到包含所有值的所有可能组合的最佳集合

R找到包含所有值的所有可能组合的最佳集合,r,select,optimization,combinations,nonlinear-optimization,R,Select,Optimization,Combinations,Nonlinear Optimization,经过谷歌的努力,我希望有人能帮助我解决这个问题,这对我来说很简单,但可能比我想象的要复杂: 我有一个三列的data.frame。前两项反映了五个变量(1-5)的所有可能组合,最后一项反映了组合的“强度”。我查找五行,其中包括Var1和Var2的所有值(因此值为1-5),并且在强度列中的总和最高。在下面的示例中,这是强度为1000的五行,因为它们的总和最高,所有五个值(1-5)都在前两列中给出 我怎样才能最好地解决这个问题?是否有实现该任务的包?我现在找到了constrOptim()函数,我可以用

经过谷歌的努力,我希望有人能帮助我解决这个问题,这对我来说很简单,但可能比我想象的要复杂:

我有一个三列的data.frame。前两项反映了五个变量(1-5)的所有可能组合,最后一项反映了组合的“强度”。我查找五行,其中包括Var1和Var2的所有值(因此值为1-5),并且在强度列中的总和最高。在下面的示例中,这是强度为1000的五行,因为它们的总和最高,所有五个值(1-5)都在前两列中给出

我怎样才能最好地解决这个问题?是否有实现该任务的包?我现在找到了constrOptim()函数,我可以用它吗

创建示例数据帧的代码:

a <-cbind(expand.grid(seq(1,5,1),seq(1,5,1)),
          strength = c(-11, 61, 230, 118, 156, 98, 169, 306, 6, -54,   
                        207, -32, 27, 128, 101, 19, -18, 32, 153, 14, 
                        63, 136, 165, 73, 35))
a <- a[order(a$strength, decreasing=T),]
非预期结果:

 Var1 Var2 strength
 3    2      306
 3    1      230
 1    3      207
 2    2      169
 3    5      165
期望的结果:

 Var1 Var2 strength
 3    2      306
 1    3      207
 5    1      156
 4    4      153
 2    5      136
考虑
Var1
Var2
列之间的一系列and:

# MERGE MAX AGGREGATES WHERE Var COL ARE EQUAL AND NOT EQUAL
mergedf1 <- merge(aggregate(strength ~ Var1, data=a[a$Var1==a$Var2,], FUN=max), 
                  a, by=c("Var1", "strength"))
mergedf2 <- merge(aggregate(strength ~ Var1, data=a[a$Var1!=a$Var2,], FUN=max), 
                  a, by=c("Var1", "strength"))

# STACK RESULTS 
mergedf <- rbind(mergedf1, mergedf2)

# FINAL MAX AGGREGATION AND MERGE
final <- merge(aggregate(strength ~ Var2, data=mergedf, FUN=max), 
               mergedf, by=c("Var2", "strength"))
final <- final[,c("Var1", "Var2", "strength")]                    # SORT COLUMNS
final <- final[with(final, order(-strength)),]                    # SORT ROWS

# REMOVE TEMP OBJECTS
rm(mergedf1, mergedf2, mergedf)
#合并变量列相等和不相等的最大聚合

mergedf1我不确定给出的解决方案是否最有效,但不知何故,我觉得我们必须检查整个数据集以找到唯一的对(例如,将
(Var1=2,Var2=5,strength=136)的值更改为
(Var1=2,Var2=5,strength=1)
。为了找到唯一的对,我使用应用函数。首先让我们重新创建输入:

a <-cbind(expand.grid(seq(1,5,1),seq(1,5,1)),
          strength = c(-11, 61, 230, 118, 156, 98, 169, 306, 6, -54,   
                        207, -32, 27, 128, 101, 19, -18, 32, 153, 14, 
                        63, 136, 165, 73, 35))
a <- a[order(a$strength, decreasing=T),]
接下来,我编写一个函数,从有序数据集
a
中获取一行,检查
Var1
Var2
是否唯一,如果是,则存储强度

mf <- function(x){
    if( !(x[1] %in% V[,1]) & !(x[2] %in% V[,2])) {
        i <- x[1]
        V[i,1] <<- x[1]
        V[i,2] <<- x[2]
        V[i,3] <<- x[3]
    }
}
所需的值存储在矩阵
V
中:

V
     [,1] [,2] [,3]
[1,]    1    3  207
[2,]    2    5  136
[3,]    3    2  306
[4,]    4    4  153
[5,]    5    1  156
有时,虽然没有必要检查完整的数据集(如所给出的示例),但我们希望在找到唯一的数据对后能够中断循环。为此,我们可以使用
For
循环。以下是代码:

a <-cbind(expand.grid(seq(1,5,1),seq(1,5,1)),
          strength = c(-11, 61, 230, 118, 156, 98, 169, 306, 6, -54,   
                       207, -32, 27, 128, 101, 19, -18, 32, 153, 14, 
                       63, 136, 165, 73, 35))
a <- a[order(a$strength, decreasing=T),]

V <- matrix(nrow=5,ncol=3)
for (i in 1:nrow(a)) {
    if( sum(is.na(V[,1])) == 0)
        break
    if( !(a[i,1] %in% V[,1]) & !(a[i,2] %in% V[,2])) {
        j <- a[i,1]
        V[j,1] <- a[i,1]
        V[j,2] <- a[i,2]
        V[j,3] <- a[i,3]
    }
}

a您能显示预期的输出吗?问题在于“第一列和第二列中使用了所有变量…”。这意味着什么?最终解决方案应该有五行。前两列(v1、v2)两者都应该有从1到5的所有值。这说明了吗?我可以稍后发布一个我知道解决方案的示例。这个示例是随机的,因此我不知道解决方案。谢谢!如果你不知道答案应该是什么样子,你怎么知道它是否正确?问题似乎是如何排序和获取前5个答案结果:set.seed(4444)a谢谢,但我必须重新制定问题,使其反映我的问题。请参阅上面新的所需输出。发生了什么变化?我刚刚使用了数据帧示例并运行了上面的代码。最终结果与所需结果匹配。只需删除
行。名称()
line。您好,对不起,我的示例不太好。我制作了一个新的示例。问题是每个值(1-5)都应该在Var1和Var2列中可用。如果我只是简单地按强度排序,则不一定会给出。尽管在特定情况下是可能的。请参阅使用
aggregate()
merge()
组合进行更新。
apply(a, 1, mf)
V
     [,1] [,2] [,3]
[1,]    1    3  207
[2,]    2    5  136
[3,]    3    2  306
[4,]    4    4  153
[5,]    5    1  156
a <-cbind(expand.grid(seq(1,5,1),seq(1,5,1)),
          strength = c(-11, 61, 230, 118, 156, 98, 169, 306, 6, -54,   
                       207, -32, 27, 128, 101, 19, -18, 32, 153, 14, 
                       63, 136, 165, 73, 35))
a <- a[order(a$strength, decreasing=T),]

V <- matrix(nrow=5,ncol=3)
for (i in 1:nrow(a)) {
    if( sum(is.na(V[,1])) == 0)
        break
    if( !(a[i,1] %in% V[,1]) & !(a[i,2] %in% V[,2])) {
        j <- a[i,1]
        V[j,1] <- a[i,1]
        V[j,2] <- a[i,2]
        V[j,3] <- a[i,3]
    }
}