多维数组插入数据帧 [英] Multidimensional array into data frame
问题描述
R中的以下问题对你们中的许多人来说似乎很容易,但是由于我对此还比较陌生,因此如果您能帮助我,将非常有帮助.我本质上想写一个多维(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.
我正在使用NetCDF文件,其中包含2015年1月1日至01月1日的每月海表温度(SST)数据.从文件中提取SST变量(使用ncdf4软件包)会生成一个多维数组(经度= 360,纬度= 180,时间= 1992)(基本上是全局地图图层,跨时间矢量堆叠,也包含NA值-在陆地上温度).
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).
我想要的是一个数据帧,其中:第一列是经度,第二纬度,第三时间,第四SST值.我的问题是维数的长度不同,我看不到如何使R理解它需要正确地分解数据.
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.
我想要的例子如下:
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!
推荐答案
我们可以使用melt
library(reshape2)
melt(arrayObj)
这篇关于多维数组插入数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!