Getting error in R outputs

I am writing an R code for finding the tensions in my soil profile. I have 100 text files. Cannot go through them manually one by one. The first column of text file is time in days and second is soil tensions; I am getting an error "Error in sprintf("%04.f", i) : object 'i' not found". I cannot understand what is going on. Can someone help.

Here is what the short code looks like:

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()"

Maybe this helps.
In Part1 I create 2 (instead of your 100) input files.
In Part2 I read them in the way you would like to handle them (I think).

# Part 1

df1 = data.frame(
  time = c(9,10,11,12), 
  tension  = c(3,2,1,1)
  ) 

filename = 'inputs_0001.txt'
write.csv(df1,filename,row.names = FALSE)

df2 = data.frame(
  time = c(9,10,11,12), 
  tension  = c(5,4,3,2)
  ) 

filename = 'inputs_0002.txt'
write.csv(df2,filename,row.names = FALSE)

# Part 2

threshold = 1.5
numfiles = 100

output = matrix(data=NA,nrow=numfiles,ncol=3)

#for (i in 1:numfiles) {
for (i in 1:2) {
  tension = read.csv(sprintf("inputs_%04.f.txt", i))
  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
  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.
  
} 

head(output)
#>      [,1] [,2] [,3]
#> [1,]    1   11   11
#> [2,]    2   12 1200
#> [3,]   NA   NA   NA
#> [4,]   NA   NA   NA
#> [5,]   NA   NA   NA
#> [6,]   NA   NA   NA

Created on 2020-07-05 by the reprex package (v0.3.0)

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