以下は正常に動作し、今日は突然警告を思い付いたコードで置き換え長の倍数ではありませんdf1_Drop [ID_posin1] < - df2_Pick [ID_posin2]交換するアイテムの数は
コードはで使用されている次の行
警告が表示され、出力が正しくない理由が見つかりません。
Cost_match<-match_cost(SingleValueDistribute = F,df1_ID = Net_Rev$`Man.ID`,
df2_ID = Production_cost$Man.ID,
df2_Pick = Production_cost$Man.Revenue,
df1_Drop = Net_Rev$`Man.Revenue`,
df1_Dist_by = Net_Rev$Revenue,dist = T,weighted = T)
警告メッセージ:df1_Drop [ID_posin1] <で - df2_Pick [ID_posin2]:交換するアイテムの
数はあなたが持っている交換用長
match_cost<-function(SingleValueDistribute=F, df1_ID,df2_ID,df2_Pick,df1_Drop,df1_Dist_by,weighted=F,dist=F){
# SingleValueDistribute allows to distribute a single value across many rows
# IDs not needed in this case
if(SingleValueDistribute==T) {
sum<-sum(df1_Dist_by)
perc<-df1_Dist_by/sum
cost<-df2_Pick
df1_Drop<-perc*cost
reps<-NULL
print(" Singular Value Distributed")
}else{
df<-data.frame(table(df1_ID))
df<-df[which(df$df1_ID %in% df2_ID),]
reps<-as.character(unique(df$df1_ID[which(df$Freq>1)]))
if (length(reps)>0 & dist==F) {
print("Multiple IDs; Values not Distributed")
} else if(length(reps)>0 & dist==T & weighted==T){
for(i in df2_ID){ #Loop to distribued by df1_Dist_by
rows<-which(df1_ID==i)
sum<-sum(df1_Dist_by[rows])
cost<-df2_Pick[which(df2_ID==i)]
if(sum==0){
df1_Drop[rows][1]<-cost
}else{
perc<-df1_Dist_by[rows]/sum
df1_Drop[rows]<-perc*cost
}
}
print("Multiple IDs; Value Weighted and Distributed")
}else{
# Direct matching
unique_ID<-unique(df2_ID) #Get unique IDs
#Find positions of unique IDs the two data sets
ID_posin1<- match(x = unique_ID, table = df1_ID, nomatch = 0) # Find position of unique Ids
ID_posin2<- match(x = unique_ID, table = df2_ID, nomatch = 0) # Find position of unique Ids
# Find corresponding cost positions
df1_Drop[ID_posin1]<-df2_Pick[ID_posin2]
if(length(reps)>0) print("Multiple IDs; Values singularly Distributed") else
print("Singular IDs; Values Distributed")
}
}
return(list(df1_Drop=df1_Drop,rep_ID=reps))
}
データセットは同じで、私はdf2がすべてのデータをdf1.Soのsolution.Ifを探していると言ったときに、仮定を保持していることを手動でチェックしました。ループを作成する方法は他にありますチェックは素晴らしいでしょう。加重に基づくデータの分布。 –
@ArkadeepPaulChoudhury私はより堅牢でなければならない代替コードを提案しました。 – CSJCampbell
@CSJCambell私はまだ問題に直面している機能を書き直せますか?何かが欠落しています。あなたが言うことを補うことができないために残念です。 'Cost_match <-match_cost(SingleValueDistribute = F、df1_ID = Net_Rev $ 'Man.ID'、 df2_ID = Production_cost $ Man.ID、 df2_Pick = Production_cost $ Man.Revenue、 df1_Drop = Net_Rev $' Man.Revenue' 、 df1_Dist_by = Net_Rev $ Revenue、dist = T、weighted = T) ' –