script about the R program

Asked

Viewed 140 times

1

I am using R to extract data in format .h5. I’m getting it done. However, they are data of 200 years, and monthly.

For now, I can only call through the program a file every month at a time, but I need to be able to make a script where I can extract the data from each file for every year, without having to do it one by one. My script is like this:

library(hdf5)
mydata = hdf5load ("teste_200-Q-2000-01-00-000000-g01.h5",load=FALSE)
mydata$AGB_PY
names(mydata)

I tried to concatenate and it was like this:

library(hdf5)
ED<-c ( "01.h5","02.h5","03.h5","04.h5","05.h5","06.h5", "07.h5", "08.h5", "09.h5","10.h5","11.h5","12.h5")
ED
names(mydata)
for(i in 01:12)sum(mydata$AGB_PY)
resu<-sum(mydata$AGB_PY)
resu
agbyear = rep(NA,times=12)
for (i in 1:12){
  mydata = hdf5load(ED[i],load=FALSE)
  agbyear[i] = sum(mydata$AGB_PY)}
agbyear
mydata$AGB_PY<- edit(data.frame(agbyear))
write.table(agbyear,"agbyear.csv", row.names=FALSE  , sep  = ",")

But I wanted to know how to make him know that he has to call all the files .h5 and distinguish between months and years.

Editing:

Answering the questions, the format of each file is this:

teste_200-Q-2001-01-00-000000-g01.h5
teste_200-Q-2001-02-00-000000-g01.h5
teste_200-Q-2001-03-00-000000-g01.h5

... and so on , for 200 years, from 2000 to 2200.

So, as the month gets in the "middle" of the file name, how could I call tosos the archives of the month?

I tried it like this: "_. H5" but it didn’t work. I also tried "*. H5" didn’t work either.

2 answers

2

Your code seems to be almost 100%, missed just you assemble the name of each file completely. You can do this using the function paste, or paste0 not to need to define the argument sep = "".

Solving just for the months, that you had already started, and removing the lines that you put just to observe the data, would look like this:

library(hdf5)

ED <- c("01.h5","02.h5","03.h5","04.h5","05.h5","06.h5", "07.h5", "08.h5", "09.h5","10.h5","11.h5","12.h5")

resu <- sum(mydata$AGB_PY)
agbyear <- rep(NA, times=12)
basename <- "teste_200-Q-2000-01-00-000000-g"

for (i in 1:12) {
  mydata <- hdf5load(paste0(basename, ED[i]), load=FALSE)
  agbyear[i] <- sum(mydata$AGB_PY)
}

write.table(agbyear, "agbyear.csv", row.names=FALSE, sep = ",")

Some points that can be highlighted:

  1. Perhaps it is not best to manually type the end of the files into an array (or create all the names). An output would be to use something like list.files(pattern="\\.h5"), which returns an array with the filenames .h5 in Working directory.

  2. Avoid using edit(). If you change the data manually, the code is irreproducible. Look for ways to make the changes you want with code.

  3. Instead of for, we could use a sapply, which is a more idiomatic way of being do the same thing, without having to create the vector agbyear formerly.

For example:

allfiles <- paste0(basename, ED)
agbyear <- sapply(allfiles, function(i) {
  mydata <- hdf5load(i, load=FALSE)
  sum(mydata$AGB_PY)
}

Despite the point 1, we can grab the hook from sapply and of allfiles to create all the files for several years, for several months. It was unclear on the question whether the months really are the part in ED (I imagine so), and what is the interval of years. But for the interval 2000 - 2015, we could do so:

allfiles <- as.vector(sapply(anos, function(a) {
  paste0("teste_200-Q-", a, "-01-00-000000-g", ED)
}))

Thus, we would have 16*12 = 192 filenames. But if the files are organized, maybe the best option is still to use the list.files().

1

I’m gonna try to help you and I need some information. How do you define the month and year? What is the default file name? What data is contained in this AGB_PY component?

You can do something like:

    # Função para ler os arquivos h5, recebendo um diretório como argumento.

    lerH5 <- function( diretorio ){
    # sempre caregar a biblioteca hdf5load quando chamar a função
    require(hdf5)

    listaArquivos <- list.files( diretorio )

      for( i in 1:length(listaArquivos)){
         #realizar a extração dos dados que deseja aqui, para cada arquivo
      }
    }

I hope I have contributed in some way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.