循环以执行R中的许多文件



我很抱歉问这个问题,因为我真的是R的新手。我有很多名称相似的数据,例如data_40231_2019data_40232_2019等等。我想运行这些数据,通过使用(Benchmark(和循环来查找效率。我用于输入的变量名是real_40231_2019,输出是vol_40231_2019。正如您所看到的,变量名跟在数据名后面。有没有办法循环这个脚本?

下面是我目前的剧本;一个接一个。此外,我想将其保存为类似的名称。

setwd   ("path")
data    <- read.csv(file="data_40231_2019.csv", header=T, sep=",")
data

# input
x1=data$real_40231_2019
x=matrix(c(x1),length(x1),1);
x

# output
y1=data$vol_40231_2019
y=matrix(c(y1),length(y1),1);
y

# DEA
efficiencies <- dea(x,y,RTS="vrs", ORIENTATION="in")
efficiencies
dea.plot.frontier(x,y,RTS="vrs")

# summary
efficiencies(efficiencies)
print(efficiencies)
summary(efficiencies)
efficiencies

# table
DMU=data$dmu
efficiencies <-c(efficiencies(efficiencies))
efficiency <-cbind(DMU, efficiencies)
efficiency

# save
file = "path"
write.csv (efficiency, file="Efisiensi_40231_2019.csv") 

谢谢。

for (this_file in list.files(pattern = 'data.*\.csv', full.names = F)){

this_str <- gsub('data_(.*)\.csv','\1',this_file); # 40232_2019
this_str_real <- paste0('real_',this_str) # real_40232_2019
this_str_vol <- paste0('vol_',this_str) # vol_40232_2019

data    <- read.csv(file=this_file, header=T, sep=",")

# input
x1=data[[this_str_real]]
x=matrix(c(x1),length(x1),1);

# output
y1=data[[this_str_vol]]
y=matrix(c(y1),length(y1),1);

# DEA
efficiencies <- dea(x,y,RTS="vrs", ORIENTATION="in")
png(filename = paste0(this_str,'_eff_plot.png'))
dea.plot.frontier(x,y,RTS="vrs")
dev.off()


# CHANGE THE NEXT TWO SECTIONS IF YOU WANT TO SAVE OUTPUT IN THE FILE
# YOU CAN USE THE LAST LINE (write.csv()) AS EXAMPLE

# summary
efficiencies(efficiencies)
print(efficiencies)
summary(efficiencies)
efficiencies

# table
DMU=data$dmu
efficiencies <-c(efficiencies(efficiencies))
efficiency <- cbind(DMU, efficiencies)
efficiency

# save
file = "path"
write.csv(efficiency, file=paste0('Efisiensi_', this_str,".csv")) 
}

相关内容

  • 没有找到相关文章

最新更新