Convert rasters to data frame with time stamp
I don't think I completely understand but can you use tidyr::gather to get
what you want? if you have multiple datasets you could join them all
together at the end.
library(raster)
library(tidyr)
#Create a rasterStack similar to my data - same dimensions and layer names
r <- raster(ncol=60, nrow=60)
s <- stack(lapply(1:408, function(x) setValues(r, runif(ncell(r)))))
names(s) <- paste0('X', seq(as.Date("1980/1/1"), by = "month", length.out =
408))
s
dF=as.data.frame(s)
dF2=gather(dF,date)
On Fri, Oct 16, 2015 at 2:39 PM, Thiago V. dos Santos <
thi_veloso at yahoo.com.br> wrote:
Dear list,
Generally speaking, I love R. However, one of the things I like least in R
is the need to interchange between the various data formats required by
different packages.
I am trying to apply a bias-correction function on some gridded climate
data. The qmap package has functions to perform bias correction on climate
data, but the problem I am grasping with is that it requires data to be
organized as data.frames:
library(qmap)
data(obsprecip)
data(modprecip)
#Fit a quantile mapping function to observed and modeled data
qm.fit <- fitQmap(obsprecip,modprecip,
method="QUANT",qstep=0.01)
#Perform bias correction on modeled data
qm <- doQmap(modprecip, qm.fit, type="tricub")
And that's all. But notice that both observed and modeled data in this
example are data frames for different locations (Moss, Geiranger and
Barkestad):
head(obsprecip)
MOSS GEIRANGER BARKESTAD 1-1-1961 0.1 0 0 2-1-1961 0.2 0 0 3-1-1961 0.9 0 0 4-1-1961 10.6 0 0 5-1-1961 1.5 0 0 6-1-1961 1.2 0 2
head(modprecip)
MOSS GEIRANGER BARKESTAD
2-1-1961 2.283 0.0000 3.177000
3-1-1961 2.443 10.8600 1.719000
4-1-1961 3.099 12.7300 6.636000
5-1-1961 0.000 9.7720 9.676000
6-1-1961 0.140 0.6448 7.110000
7-1-1961 13.470 3.3570 0.001107
Now, let's back to my problem. I have monthly precip data to which I want
to apply the same function above, but my data is gridded:
library(raster)
#Create a rasterStack similar to my data - same dimensions and layer names
r <- raster(ncol=60, nrow=60)
s <- stack(lapply(1:408, function(x) setValues(r, runif(ncell(r)))))
names(s) <- paste0('X', seq(as.Date("1980/1/1"), by = "month", length.out
= 408))
s
Therefore, I need to load data as rasters and iterate through all
individual gridcells to create a data frame containing:
date1, cell1, cell2, cell3, ..., cell3600
date2, cell1, cell2, cell3, ..., cell3600
date3, cell1, cell2, cell3, ..., cell3600...
date408, cell1, cell2, cell3, ..., cell3600
then apply the fit function and finally convert the data back to a raster.
Any ideas on how to efficiently convert rasters to data frames containing
their time stamp and then back to a raster again??
Any hint is much appreciated.
Greetings,
-- Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo