我觉得这里的解决方案可能很容易,所以如果我的问题是基本的,我很抱歉提前,但我是R的新手,我真的需要这个脚本工作。
我试图使用预先编写的脚本提取一些数据,该脚本依赖于软件特定的R包(bin在程序中生成为报告,我需要这些表格格式的报告)。
这是我使用的代码:
*setwd("C:/Users/Oo/OneDrive/Documents/Evil_code_testing/")
minDateFilea <- '1991-01-01'
minDateFileb <- '2021-01-01'
firstYearAssessment <- '1991'
lastYearAssessment <- '2050'
RAC <- 0.0196
print('_____________________________________')
print('Defining function taking file paths and returning data')
getBinData <- function(patha, pathb) {
filea<-macroReadBin(patha)
fileb<-macroReadBin(patha)
nameRun<- substring(patha, (stri_locate_last_fixed(patha, "par")[,2] +1), (stri_locate_last_fixed(patha, ".bin")[,1]-2))
#Extract results for a and b
selectedfilea <- filea[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
selectedfileb <- fileb[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
#Reject the first 2 years
selectedfilea<-selectedfilea %>% filter(Date>minDateFilea)
selectedfileb<-selectedfileb %>% filter(Date>minDateFileb)
#Combine
selectedfile <-rbind(selectedfilea,selectedfileb)
#Summarise max PEC per year
selectedfile$PECsw <-((selectedfile$Drainage_mmh_Rate_total*selectedfile$Solute_concentration_Drain_flow*24)/(selectedfile$Drainage_mmh_Rate_total*24+3))
selectedfile$YEAR<-paste0(selectedfile$Date)
selectedfile$YEAR<-as.POSIXct(strptime(selectedfile$YEAR, format ="%Y-%m-%d %H:%M:%S", tz = "GMT"))
selectedfile$YEAR<-format(selectedfile$YEAR, format = "%Y")
selectedfile_stat<-selectedfile %>% group_by(YEAR) %>% summarise_at(vars(PECsw), list(max=max))
names(selectedfile_stat)[names(selectedfile_stat) == 'max'] <- nameRun
selectedfile_stat
}
print('_____________________________________')
print('Grab all bin files')
#Get a list of the files
files <- list.files(getwd(), ".bin")
soil<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +1), (stri_locate_last_fixed(files, "par")[,2] +1))
weather<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +2), (stri_locate_last_fixed(files, ".bin")[,1]-2))
part<-substring(files, (stri_locate_last_fixed(files, ".bin")[,1]-1), (stri_locate_last_fixed(files, ".bin")[,1]-1))
filesToTreat <- data.frame("Path" = files,"Soil" = soil, "Weather" = weather, "Part" = part)
print('_____________________________________')
print('match a/b together to determine selected scenarios')
filesToTreat$Matching_b<-paste(filesToTreat$Part)
for(i in 1:length(filesToTreat$Matching_b)){
if((filesToTreat$Part[i] =="a")&(filesToTreat$Part[i+1] =="b")&(filesToTreat$Soil[i+1] ==filesToTreat$Soil[i])&(filesToTreat$Weather[i+1] ==filesToTreat$Weather[i])){
filesToTreat$Matching_b[i] = filesToTreat$Path[i+1]
}else{filesToTreat$Matching_b[i] = "FALSE"}
}
print(filesToTreat)
print('_____________________________________')
print('Extract and merge selected scenarios together')
#create an empty dataframe
OriginalDataframe<- data.frame("YEAR" = numeric(0))
#for each scenario...
for(i in 1:length(filesToTreat$Matching_b)){
if(filesToTreat$Matching_b[i] != "FALSE"){
#extract drainflow rate and concentration data from bin files a&b and determine maxPEC per year
xpatha<- filesToTreat$Path[i]
xpathb<- filesToTreat$Matching_b[i]
xselectedfile_stat <- getBinData(xpatha, xpathb)
#Merge maxPEC data together
OriginalDataframe <-merge(xselectedfile_stat,OriginalDataframe,by="YEAR", all = TRUE)
print(paste('added data for soil', filesToTreat$Soil[i], 'and weather', filesToTreat$Weather[i]))
}
}*
这里是导致错误的结果和错误后,它都是不可用的
> setwd("C:/Users/Oo/OneDrive/Documents/Evil_code_testing/")
> RAC <- 0.0196
>
> print('_____________________________________')
[1] "_____________________________________"
> print('Defining function taking file paths and returning data')
[1] "Defining function taking file paths and returning data"
>
>
> getBinData <- function(patha, pathb) {
**+ filea<-macroReadBin(patha)
+ fileb<-macroReadBin(patha)**
+ nameRun<- substring(patha, (stri_locate_last_fixed(patha, "par")[,2] +1), (stri_locate_last_fixed(patha, ".bin")[,1]-2))
+ #Extract results for a and b
+ selectedfilea <- filea[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
+ selectedfileb <- fileb[c('Date','Drainage_mmh_Rate_total','Solute_concentration_Drain_flow')]
+ #Reject the first 2 years
+ selectedfilea<-selectedfilea %>% filter(Date>minDateFilea)
+ selectedfileb<-selectedfileb %>% filter(Date>minDateFileb)
+ #Combine
+ selectedfile <-rbind(selectedfilea,selectedfileb)
+ #Summarise max PEC per year
+ selectedfile$PECsw <-((selectedfile$Drainage_mmh_Rate_total*selectedfile$Solute_concentration_Drain_flow*24)/(selectedfile$Drainage_mmh_Rate_total*24+3))
+ selectedfile$YEAR<-paste0(selectedfile$Date)
+ selectedfile$YEAR<-as.POSIXct(strptime(selectedfile$YEAR, format ="%Y-%m-%d %H:%M:%S", tz = "GMT"))
+ selectedfile$YEAR<-format(selectedfile$YEAR, format = "%Y")
+ selectedfile_stat<-selectedfile %>% group_by(YEAR) %>% summarise_at(vars(PECsw), list(max=max))
+ names(selectedfile_stat)[names(selectedfile_stat) == 'max'] <- nameRun
+ selectedfile_stat
+ }
> print('_____________________________________')
[1] "_____________________________________"
> print('Grab all bin files')
[1] "Grab all bin files"
> #Get a list of the files
> files <- list.files(getwd(), ".bin")
> soil<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +1), (stri_locate_last_fixed(files, "par")[,2] +1))
> weather<-substring(files, (stri_locate_last_fixed(files, "par")[,2] +2), (stri_locate_last_fixed(files, ".bin")[,1]-2))
> part<-substring(files, (stri_locate_last_fixed(files, ".bin")[,1]-1), (stri_locate_last_fixed(files, ".bin")[,1]-1))
> filesToTreat <- data.frame("Path" = files,"Soil" = soil, "Weather" = weather, "Part" = part)
>
> print('_____________________________________')
[1] "_____________________________________"
> print('match a/b together to determine selected scenarios')
[1] "match a/b together to determine selected scenarios"
> filesToTreat$Matching_b<-paste(filesToTreat$Part)
> for(i in 1:length(filesToTreat$Matching_b)){
+ if((filesToTreat$Part[i] =="a")&(filesToTreat$Part[i+1] =="b")&(filesToTreat$Soil[i+1] ==filesToTreat$Soil[i])&(filesToTreat$Weather[i+1] ==filesToTreat$Weather[i])){
+ filesToTreat$Matching_b[i] = filesToTreat$Path[i+1]
+ }else{filesToTreat$Matching_b[i] = "FALSE"}
+ }
> print(filesToTreat)
Path Soil Weather Part Matching_b
1 parL109082WCa.bin L 109082WC a 2
2 parL109082WCb.bin L 109082WC b FALSE
3 parL111074WCa.bin L 111074WC a 4
4 parL111074WCb.bin L 111074WC b FALSE
5 parL129077WCa.bin L 129077WC a 6
6 parL129077WCb.bin L 129077WC b FALSE
>
> print('_____________________________________')
[1] "_____________________________________"
> print('Extract and merge selected scenarios together')
[1] "Extract and merge selected scenarios together"
> #create an empty dataframe
> OriginalDataframe<- data.frame("YEAR" = numeric(0))
> #for each scenario...
> for(i in 1:length(filesToTreat$Matching_b)){
+ if(filesToTreat$Matching_b[i] != "FALSE"){
+ #extract drainflow rate and concentration data from bin files a&b and determine maxPEC per year
+ xpatha<- filesToTreat$Path[i]
+ xpathb<- filesToTreat$Matching_b[i]
+ xselectedfile_stat <- getBinData(xpatha, xpathb)
+ #Merge maxPEC data together
+ OriginalDataframe <-merge(xselectedfile_stat,OriginalDataframe,by="YEAR", all = TRUE)
+ print(paste('added data for soil', filesToTreat$Soil[i], 'and weather', filesToTreat$Weather[i]))
+ }
+ }
Error in UseMethod("macroReadBin") :
no applicable method for 'macroReadBin' applied to an object of class "factor"
我把我认为导致问题的行加粗,但我不知道如何解决它。在WD中,我调用的文件名为000a,000b,999a,999b等,所以我试图将readbinmacro应用于特定的文件名,但它没有改变任何东西。
我真的需要了解这段代码,所以我真的会很感激,如果有人能告诉不只是如何修复它,但我从根本上错误,所以我可以避免在未来这一点。这段代码是写的人,我没有办法联系,所以不幸的是不能检查与作者
1条答案
按热度按时间hwamh0ep1#
解决方案简单得可笑。如果有人遇到类似的问题,这里是有帮助的