问题
The following issue in R might seem easy to many of you, but since I am relatively new to this, it would be extremely helpful if you could help me. I want to essentially write a multidimensional (3 dims) array as data frame that I can more easily manipulate.
I am working with a NetCDF file of monthly Sea Surface Temperature (SST) data for the period of 01/01/1891-01/12/2015. Extracting the SST variable from the file (using the ncdf4 package) results in a multidimensional array (longitude = 360,latitude = 180 ,time = 1992)(Basically global map layers,stacked across the time vector,including NA values too - on land temperatures).
What I would like to have instead is a data frame, in which: the first column is the Longitude, second Latitude, third Time, fourth SST values. My problem is that the dimensions are not of the same length and I cannot see how I can make R understand that it needs to un-stack the data properly.
An example of what I want would look like:
Longitude Latitude Time SST
0,5 89.5 01/01/1891 1.25
0.5 89 01/01/1891 1.27
0.5 88.5 01/01/1891 1.28
… … … …
1 89.5 01/01/1891 1.28
1 89 01/01/1891 1.29
1 88.5 01/01/1891 1.26
… … … …
0.5 89.5 01/02/1891 1.26
0.5 89 01/02/1891 1.28
… … … …
Thank you so much for your time and patience!
回答1:
We can use melt
library(reshape2)
melt(arrayObj)
来源:https://stackoverflow.com/questions/40840660/multidimensional-array-into-data-frame