3

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!

George Pexas
  • 53
  • 1
  • 3
  • 1
    TRy with `library(reshape2); melt(yourarray)` – akrun Nov 28 '16 at 09:15
  • 1
    **Worked perfectly!!** Thank you so very much. I honestly don't know why I felt sure it wouldn't be able to handle it that easily, although I knew about this function. @akrun – George Pexas Nov 28 '16 at 12:11

1 Answers1

4

We can use melt

library(reshape2)
melt(arrayObj)
akrun
  • 874,273
  • 37
  • 540
  • 662