[R] [newbie] convert 3D spatial array to dataframe

Tom Roche Tom_Roche at pobox.com
Sun Nov 18 07:00:24 CET 2012


https://stat.ethz.ch/pipermail/r-help/2012-November/329438.html
>> summary: how to convert a 3D array (as obtained from ncdf4::ncvar_get)
>> to a dataframe suitable for use by lattice::levelplot(), e.g.,

>> levelplot(conc ~ lon * lat | lev, data = data.frame)

much detail omitted ...

>> I'm guessing this involves function=reshape

https://stat.ethz.ch/pipermail/r-help/2012-November/329439.html
> an arguably easier way:
> dimnames(array.3d) <- list(lat= 1:7 , long = 1:11 , lev = 1:5)
> levelplot(array.3d)

I know levelplot will do arrays directly, but I don't see how to get the
naming I want without a dataframe: am I missing something?

Thanks to zero_one for offlist pointer to reshape2::melt:

library(reshape2)
data.frame <-
  melt(array.3d, varnames=c("lon", "lat", "lev"), value.name="conc")
library(lattice)
levelplot(conc ~ lon * lat | factor(lev), data = data.frame)

works!

thanks all, Tom Roche <Tom_Roche at pobox.com>




More information about the R-help mailing list