数据帧记录的行数中的错误,以便在计算中忽略该记录

时间:2017-08-26 15:49:44

标签: r dataframe

我有一个数据框,可以进行一些计算并计算一个对象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

非常感谢这方面的任何帮助

2 个答案:

答案 0 :(得分:1)

我不熟悉您的分析,但您似乎正在使用rbind来合并数据。如果行名称不符合您的预期,您可以在aux1<- rbind(aux1,c(0*rep(1,m),as.numeric(inputs[i,])))行之后执行以下操作。

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

这将确保行名称与行号相同。

答案 1 :(得分: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行。