Getting error in R program code

rm(list=ls())
library(lattice)

setwd("C:\Users\Public\Documents\PC-Progress\Hydrus-1D 4.xx\Examples\Direct\ATNKCatotelmonlyWTD5cm") # Set home directory
getwd() #Check where R is looking

num <- 5000 #Number of realisations
threshold <- -100 #Threshold tension

output <- array(0, dim=c(num,3)) #set output directory

for(i in 1:num){

if(file.info (paste0("C:\Users\Public\Documents\PC-Progress\Hydrus-1D 4.xx\Examples\Direct\ATNKCatotelmonlyWTD5cm//obs_theta_",sprintf("%04.f",i),".out"))$size > 0){

  tension <- read.table(paste0("C:\\Users\\Public\\Documents\\PC-Progress\\Hydrus-1D 4.xx\\Examples\\Direct\\ATNKCatotelmonlyWTD5cm//obs_tension_",sprintf("%04.f",i),".out"))    #load table of increasing file numbers


 output[i,1] <- min(tension[,2])   #minimum pressure of surface node
 output[i,2] <- tension[which.min(tension[,2]),1]   #time of minimum tension

  x <- dim(tension)     #Number of rows of data in output file
 value <- 0            #Starting tension 


 if (min(tension[,2])<threshold){    #if threshold tension is exceed at some point during the simulations
     for (k in 1:x[1]) {             #Go through each tension measurement in time order to find when the threshold is exceeded
          if(tension[k,2]<threshold) {  #If pressure is less than the threshold value
              output[i,3] <- tension[k,1] # output time when threshold is crossed
               break
           }
     }
  }    
 else{output[i,3] <- 50*24}  #output zero if pressure is not exceeded on that run.
 rm("tension")  # close current tension file ready to open the enxt one

} else{
output[i,2] <- "=NA()"
output[i,3] <- "=NA()"

}

}

colnames(output) <- c("MinPressure","TimeOfMinPressure","TimeOfThresholdPressure") #Define column headings of the output file
write.table(output,file = "Output.txt",sep = " ", quote = FALSE, append = FALSE, na = "NA") output data

I am getting an error running this code; the errors "Error in sprintf("%04.f", i) : object 'i' not found" and another error is Error: object 'output' not found. Can anyone help.

I do not see why i should not exist but I do see other possible problems. I expect the back slashes in your file path to cause problems and, in any case, the full file path should not be needed to read in the files since you have set the working directory. Try this simplified code.

setwd("C:\Users\Public\Documents\PC-Progress\Hydrus-1D 4.xx\Examples\Direct\ATNKCatotelmonlyWTD5cm")
for ( i in 1:2) {
  INFO <- file.info (paste0("obs_theta_",sprintf("%04.f",i),".out"))
  print(INFO$size)
}

Does that work?

Where should is file.info and where should I paste/insert the lines you provided.

rm(list=ls())
library(lattice)

setwd("C:\Users\Public\Documents\PC-Progress\Hydrus-1D 4.xx\Examples\Direct\ATNKCatotelmonlyWTD5cm") # Set home directory
getwd() #Check where R is looking

num <- 5000 #Number of realisations
threshold <- -100 #Threshold tension

output <- array(0, dim=c(num,3)) #set output directory

for(i in 1:2)
{INFO <-file.info (paste0("obs_theta_",sprintf("%04.f",i),".out"))
print (INFO$size)}

  tension <- read.table(paste0("C:\\Users\\Public\\Documents\\PC-Progress\\Hydrus-1D 4.xx\\Examples\\Direct\\ATNKCatotelmonlyWTD5cm//obs_tension_",sprintf("%04.f",i),".out"))    #load table of increasing file numbers


 output[i,1] <- min(tension[,2])   #minimum pressure of surface node
 output[i,2] <- tension[which.min(tension[,2]),1]   #time of minimum tension

  x <- dim(tension)     #Number of rows of data in output file
 value <- 0            #Starting tension 


 if (min(tension[,2])<threshold){    #if threshold tension is exceed at some point during the simulations
     for (k in 1:x[1]) {             #Go through each tension measurement in time order to find when the threshold is exceeded
          if(tension[k,2]<threshold) {  #If pressure is less than the threshold value
              output[i,3] <- tension[k,1] # output time when threshold is crossed
               break
           }
     }
  }    
 else{output[i,3] <- 50*24}  #output zero if pressure is not exceeded on that run.
 rm("tension")  # close current tension file ready to open the enxt one

} else{
output[i,2] <- "=NA()"
output[i,3] <- "=NA()"

}

}

colnames(output) <- c("MinPressure","TimeOfMinPressure","TimeOfThresholdPressure") #Define column headings of the output file
write.table(output,file = "Output.txt",sep = " ", quote = FALSE, append = FALSE, na = "NA") #output data

There are 5000 text files, I had written this code 5 years ago and then did not used it for long time. Each of the txt file containes time and theta and tension values i.e. time in the first column and tension in the first, second, third and fourth column. I want the R to out the first column of the theta and tension and time at which the tension reaches -100 cm. I have inserted that info you suggested and worked but not the way I wanted.

The code I posted was intended as a small test to be run on its own. Try running this version of your code. All I did is shorten the file paths. Are there any errors?

rm(list=ls())
library(lattice)

setwd("C:\Users\Public\Documents\PC-Progress\Hydrus-1D 4.xx\Examples\Direct\ATNKCatotelmonlyWTD5cm") # Set home directory
getwd() #Check where R is looking

num <- 5000 #Number of realisations
threshold <- -100 #Threshold tension

output <- array(0, dim=c(num,3)) #set output directory

for(i in 1:num){
  
  if(file.info (paste0("obs_theta_",sprintf("%04.f",i),".out"))$size > 0){
    
    tension <- read.table(paste0("obs_tension_",sprintf("%04.f",i),".out"))    #load table of increasing file numbers
    
    
    output[i,1] <- min(tension[,2])   #minimum pressure of surface node
    output[i,2] <- tension[which.min(tension[,2]),1]   #time of minimum tension
    
    x <- dim(tension)     #Number of rows of data in output file
    value <- 0            #Starting tension 
    
    
    if (min(tension[,2])<threshold){    #if threshold tension is exceed at some point during the simulations
      for (k in 1:x[1]) {             #Go through each tension measurement in time order to find when the threshold is exceeded
        if(tension[k,2]<threshold) {  #If pressure is less than the threshold value
          output[i,3] <- tension[k,1] # output time when threshold is crossed
          break
        }
      }
    }    
    else{output[i,3] <- 50*24}  #output zero if pressure is not exceeded on that run.
    rm("tension")  # close current tension file ready to open the enxt one
    
  } else{
    output[i,2] <- "=NA()"
    output[i,3] <- "=NA()"
    
  }
  
}

colnames(output) <- c("MinPressure","TimeOfMinPressure","TimeOfThresholdPressure") #Define column headings of the output file
write.table(output,file = "Output.txt",sep = " ", quote = FALSE, append = FALSE, na = "NA")

This code has to be run all at once and not line by line.

Thank you for all the kind help. but the formatted shortened code did not run.

The following code works for me with two data files that look like

1 100
2 0
3 -100
4 -200
5 -300

You files are different than that but that structure is sufficient for the code to work. The code below differs from the last one I posted in that

  1. Both file.info() and read.table() look at files whose names are built with paste0("obs_theta_",sprintf("%04.f",i),".out")
  2. The line else{output[i,3] <- 50*24} was moved up to be placed after the } that closes the preceding if statement.
num <- 2 #Number of realisations
threshold <- -100 #Threshold tension

output <- array(0, dim=c(num,3)) #set output directory

for(i in 1:num){
  
  if(file.info (paste0("obs_theta_",sprintf("%04.f",i),".out"))$size > 0){
    
    tension <- read.table(paste0("obs_theta_",sprintf("%04.f",i),".out"))    #load table of increasing file numbers
    
    
    output[i,1] <- min(tension[,2])   #minimum pressure of surface node
    output[i,2] <- tension[which.min(tension[,2]),1]   #time of minimum tension
    
    x <- dim(tension)     #Number of rows of data in output file
    value <- 0            #Starting tension 
    
    
    if (min(tension[,2])<threshold){    #if threshold tension is exceed at some point during the simulations
      for (k in 1:x[1]) {             #Go through each tension measurement in time order to find when the threshold is exceeded
        if(tension[k,2]<threshold) {  #If pressure is less than the threshold value
          output[i,3] <- tension[k,1] # output time when threshold is crossed
          break
        }
      }
    } else{output[i,3] <- 50*24}  #output zero if pressure is not exceeded on that run.
    rm("tension")  # close current tension file ready to open the enxt one
    
  } else{
    output[i,2] <- "=NA()"
    output[i,3] <- "=NA()"
    
  }
  
}

colnames(output) <- c("MinPressure","TimeOfMinPressure","TimeOfThresholdPressure") #Define column headings of the output file
write.table(output,file = "Output.txt",sep = " ", quote = FALSE, append = FALSE, na = "NA")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.