Calculate gof metrics over a list of .xlsx files with three columns (date, simulated, observed)

Hello,
I have a list of 17 tables containing precipitation data from CHIRPS and pluviometric stations.
The tables are organized as dates on the first column, simulated in the second, and observed in the third.
I want to create a for loop that calculates the hydroGOF::GOF metrics for all the tables.

Here's what I've got so far:

stations_files = list.files(wd,
full.names = TRUE,
pattern = "Est")

results <- vector(mode = "integer", length = length(stations_files))

for (i in 1:length(stations_files)){
station <- readxl::read_xlsx(stations_files[i])
gofstation <- gof(sim = station[, 2], obs = station[,3])
results[i] <- gofstation
}

Although I think I am passing the correct columns of simulated and observed data to the gof function, I keep getting the error:
Error in me.default(sim, obs, na.rm = na.rm) :
Invalid argument type: 'sim' & 'obs' have to be of class: c('integer', 'numeric', 'ts', 'zoo')

When I calculate the gof with a single table, I dont get the error regarding the class.

Thanks in advance!!

If you run the following loop, is the class of each column one of the allowed classes?

for (i in 1:length(stations_files)){
  station <- readxl::read_xlsx(stations_files[i])
  paste(stations_files[i], "Col2", class(station[[2]]), "Col3", class(station[[3]]))
}

Actually the column 2 is read as a character.

[1] "C:/Users/Rodrigo/Documents/Chirps_RO/validacao_dados_ANA_1981-2020/tabelas/Tabelas_ANA_CHIRPS_RO_1981-2020/Est_966000.xlsx Col2 character Col3 numeric"

Just a quick update:

Ive converted everything to csv, I dont know, but I find easier to work with this type of files.

I discovered some NAs in the columns, and thought that maybe this was causing the error.

So I got to this:

stations_files = list.files(wd, ".csv")

results <- vector(mode = "integer", length = length(stations_files))

for (i in 1:length(stations_files)){
station[i] <- read.csv(stations_files[i])
station[i] = station[i] (na.omit())
gofstation <- gof(sim = station[[2]], obs = station[[3]])
results[i] <- gofstation
}

And now the error is the following:
Error in stationi : attempt to apply non-function
In addition: Warning message:
In [<-.data.frame(*tmp*, i, value = list(Data = c("1981-01-01", :
provided 3 variables to replace 1 variables

Any thoughts?

These lines are probably a problem.

station[i] <- read.csv(stations_files[i])
station[i] = station[i] (na.omit())

Do you mean

station <- read.csv(stations_files[i])
station = na.omit(station)

I did try removind the brackets, but it returned an error still.

wd = ("~/Chirps_RO/validacao_dados_ANA_1981-2020/tabelas/Tabelas_ANA_CHIRPS_RO_1981-2020_csv/")
setwd = wd

stations_files = list.files(wd, ".csv", full.names = TRUE)
stations_files
results <- vector(mode = "integer", length = length(stations_files))

for (i in 1:length(stations_files)){
station <- read.csv(stations_files[i])

gofstation <- gof(sim = station[[2]], obs = station[[3]])

results[i] <- gofstation

write.csv(get(results[i]),
paste0(results[i],
".csv"),
row.names = FALSE)
}

The error I got:
Error in get(results[i]) : invalid first argument
In addition: Warning messages:
1: 'rNSE' can not be computed: some elements in 'obs' are zero !
2: 'rd' can not be computed: some elements in 'obs' are zero !
3: In results[i] <- gofstation :
number of items to replace is not a multiple of replacement length

Heres a link with some of the files Im using:
https://drive.google.com/drive/folders/1MhHCQBMIQmFQ31axYE7NhbFhWfy4OKYr?usp=sharing

thanks

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.