I have a script for opening meteorological data from a .h5 file and calculating the average windspeed (ugrd).
library(rhdf5)
windv.2014.dec <- h5read("/Users/sethparker/Documents/My_Lab/CR_met/Horizontes_2014DEC.h5", "ugrd")
a <- as.vector(windv.2014.dec)
a[which(a == 0)] = NA_character_
avg_windv.2014.dec <- mean(abs(as.numeric(na.omit(a))))
This works fine, but I have 57 of these files. I am trying to find a way to use a for loop to not have to manually change the date each time I run it. I am mainly concerned with the year changing, I do not mind doing the process 12 times. My failed attempt at a for loop is this:
for (i in 4:9)
{
windv.201i.oct <- h5read("/Users/sethparker/Documents/My_Lab/CR_met/Horizontes_201",i,"OCT.h5", "ugrd")
a <- as.vector(windv.201i.oct)
a[which(a == 0)] = NA_character_
avg_windv.201i.oct <- mean(abs(as.numeric(na.omit(a))))
}
The data is between 2014 and 2019, hence the 4:9. How do I get the variable to work in the file pathway string?
We can use paste
or sprintf
to create the path and in the OP's loop, the output gets updated on each iteration. We can create an empty list to store the output and assign the output to it
out <- vector('list', 6)
names(out) <- 4:9
for (i in 4:9){
tmp <- h5read(sprintf("/Users/sethparker/Documents/My_Lab/CR_met/Horizontes_201%dOCT.h5", i), "ugrd")
a <- as.vector(tmp)
a[which(a == 0)] = NA_character_
out[[as.character(i)]] <- mean(abs(as.numeric(na.omit(a))))
}
names(out) <- sprintf("windv.201%s.oct", names(out))