0
下面是工作正常,今天突然想出了警告代碼在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的所有數據的手動檢查。所以尋找解決方案。如果您可以讓我知道是否有任何其他方法來創建循環檢查會很好。基於權重的數據分佈。 –
@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)' –