2017-08-26 23 views
-1

我有一個數據框,它執行一些計算並計算一個對象f.con。問題是,當我打印aux1行22後面的行號是不正確的,它給出行號221而不是23.我做了一些進一步的計算,然後該行被忽略,這基本上是一個約束。錯誤的數據幀記錄的行數,以便記錄在計算中被忽略

library(utils); library(xlsx) 
library(lpSolve) # load lpSolve package previously installed 
library(lpSolveAPI) 
datadea<- structure(list(DMUS = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22), Input1Cash = c(5, 6, 
4, 8, 5, 8, 4.4, 2.6, 3.4, 3.6, 2, 3, 3, 2.6, 4, 5, 6, 4, 7, 
6, 8, 9), Input2LEV = c(4, 5, 5, 5, 6, 3, 4.4, 8, 8, 4.4, 7, 
7, 5.6, 5, 4, 3.2, 4, 3.5, 3, 2.5, 2, 2), Output1EPS = c(1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 
members = c(1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("DMUS", 
"Input1Cash", "Input2LEV", "Output1EPS", "members" 
), row.names = c(NA, 22L), class = "data.frame") 

我有下面的代碼執行一些計算:

N <- 22 # number of DMU 
s = 2 # number of inputs 
m = 1 # number of outputs 
inputs = datadea[,c(2,3)] 
outputs = datadea[,4] 
library(lpSolve) # load lpSolve package previously installed 
library(lpSolveAPI) 
f.rhs <- c(rep(0,N),1) # RHS constraints 
f.dir <- c(rep("<=",N),"=") # directions of the constraints 
aux <- cbind(-1*inputs,outputs) # matrix of constraint coefficients in (6) 
for (i in 1:N) { 
f.obj <- c(rep(0,s),t(datadea[i,4])) # objective function coefficients 
f.con <- f.con <- rbind(aux, c(unlist(datadea[i,c(2,3)]), rep(0, m))) 

results <- lp("max",f.obj,f.con,f.dir,f.rhs,scale=1,compute.sens=TRUE) 
    multipliers <- results$solution # input and output weights 
efficiency <- results$objval # efficiency score 
duals <- results$duals # shadow prices 
if (i==1) { 
weights = c(multipliers[seq(1,s+m)]) 
effcrs <- efficiency 
lambdas = duals [seq(1,N)] 
} else { 
weights <- rbind(weights,c(multipliers[seq(1,s+m)])) 
effcrs <- rbind(effcrs , efficiency) 
lambdas <- rbind(lambdas,duals[seq(1,N)]) 
} 
} 
matrix_results <- cbind(effcrs,weights,lambdas) 
rownames(matrix_results) <- rownames(datadea) 
colnames(matrix_results) <- c("efficiency",colnames(datadea)[1:(s+m)], 
rownames(datadea)) 
rownames(matrix_results) <- rownames(datadea) 

crosseffmin = matrix(0,nrow=N,ncol=N) # initialize cross efficiency matrix 
i=18 
totaloutputs <- sum(outputs) ; 
totaloutputs = totaloutputs-as.numeric(outputs[i]) 
totalinputs <- colSums(inputs) ; 
totalinputs = totalinputs-as.numeric(unlist(inputs[i,])) 
f.obj <- c(totaloutputs,as.numeric(-totalinputs)) 
aux1 <- cbind(outputs,-1*inputs); aux11 = aux1[which(row(aux1)[,1]!=i),] ; 
aux1<-aux11[1:(N-1),] 
aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,]))) 
aux1<- rbind(aux1,c(as.numeric(outputs[i]), 
effcrs[i]*as.numeric(-inputs[i,]))) 
f.con <- aux1 
print(aux1) 

在所得的數據幀AUX1,可以看到22行之後的曖昧行號可能會在下面的輸出檢查:

aux1 
    outputs Input1Cash Input2LEV 
1   1  -5.0  -4.0 
2   1  -6.0  -5.0 
3   1  -4.0  -5.0 
4   1  -8.0  -5.0 
5   1  -5.0  -6.0 
6   1  -8.0  -3.0 
7   1  -4.4  -4.4 
8   1  -2.6  -8.0 
9   1  -3.4  -8.0 
10  1  -3.6  -4.4 
11  1  -2.0  -7.0 
12  1  -3.0  -7.0 
13  1  -3.0  -5.6 
14  1  -2.6  -5.0 
15  1  -4.0  -4.0 
16  1  -5.0  -3.2 
17  1  -6.0  -4.0 
19  1  -7.0  -3.0 
20  1  -6.0  -2.5 
21  1  -8.0  -2.0 
22  1  -9.0  -2.0 
221  0  4.0  3.5 
23  1  -4.0  -3.5 

在這方面的任何幫助,將不勝感激

+0

@ycw它沒有工作,如果你讀代碼中的最後兩行,我基本上是傳遞一個對象aux1到f.con。所以你需要檢查aux1。 – Hibu

+0

你是什麼意思?我沒有改變你的任何帖子。我所做的只是添加一個'dataframe'標籤,以便其他人可以更輕鬆地找到您的帖子。 – www

+0

@ycw哦好的謝謝 – Hibu

回答

1

我對您的分析不熟悉,但似乎您正在使用rbind來合併您的數據。如果行名稱不符合您的預期,可以在aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,])))行之後執行以下操作。

rownames(aux1) <- 1:nrow(aux1) 

這將確保行名與行號相同。

+1

或將它分配給NULL,即'rownames(aux1)< - NULL' – akrun

+1

@akrun感謝分享! – www

0

問題在於你省略了第18行,然後再添加了第22行。當你在data.frame中省略第18行時,row.names並不是新的,但它們是相同的。所以18失蹤了,但22仍然存在。您可以通過編寫以下內容輕鬆解決此問題:

# .... your code here ... 
f.obj <- c(totaloutputs,as.numeric(-totalinputs)) 
aux1 <- cbind(outputs,-1*inputs); aux11 = aux1[which(row(aux1)[,1]!=i),] ; 
aux1<-aux11[1:(N-1),] 
row.names(aux1) <- 1:nrow(aux1) # this is the new line 
aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,]))) 
aux1<- rbind(aux1,c(as.numeric(outputs[i]), 
        effcrs[i]*as.numeric(-inputs[i,]))) 
f.con <- aux1 
print(aux1) 

以row.names開頭的行是新行。只需將其嵌入到當前代碼中,evreything就可以正常工作!它只是重寫row.names,所以你可以安全地添加第22行。