I want to write a loop that extracts a table from a three dimensional array and sequentially write it to a data frame as a column.
The code I have so far is
library(ncdf4)
test <- nc_open("NorESM_TREFHT_cont_mem1-60_2006-2099.nc")
temp <- ncvar_get(test, "TREFHT", count = c(144,96,60,1))
drop(temp)
lat<- ncvar_get(test, "lat")
lon <- ncvar_get(test,"lon")
lonlat <- expand.grid(lon, lat)
for (n in 1:60) {
member <- array(temp[1:144,1:96,n])
ensemble <- as.vector(member)
h <- data.frame(cbind(lonlat, ensemble[n]))
}
Currently the code works without the loop (for n = 1,2 etc.) but only combines the last run in the for loop output for n=1:60