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



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

这方面的任何帮助将不胜感激

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

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

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

问题在于您省略了第 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 行。

最新更新