R-帮助将嵌套列表(?)转换为data.frame [英] R - Help converting nested lists(?) to data.frame

查看:195
本文介绍了R-帮助将嵌套列表(?)转换为data.frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将.mat(Matlab)数据文件导入R并将其内容组织为数据框.尽管使用R.matlab包可以很容易地进行导入,但是由于最初以某种尴尬的方式组织数据,因此转换为数据帧非常困难.看起来有两个嵌套列表.到目前为止,我还无法将其转换为数据帧.

I need to import a .mat (Matlab) datafile into R and organize its contents as a data frame. While importing is straightforward by using the R.matlab package, the conversion to data frame shows to be hard since data gets originally organized in some awkward way. It looks like there are two nested lists. So far I haven't been able to convert it to a data frame.

这是我到目前为止所拥有的:

Here is what I have so far:

# Download original flux file
oldwd <- getwd()
tmp <- tempdir()
setwd(tmp)
url <- 'https://dl.dropboxusercontent.com/u/27700634/FLUX_DATA.mat'
f <- file.path(tmp, 'FLUX_DATA.mat')
download.file(url, f, method='curl')
setwd(oldwd)

# Read data using package R.matlab
library(R.matlab)
mlab <- readMat(f)

这是文件的结构:

> str(mlab)
List of 1
$ DATA:List of 16
..$ : num [1:241, 1] 220 220 220 220 220 ...
..$ : num [1:241, 1] -22 -35.2 -31.4 -20.5 -27 ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] -29.3 -25.5 -33.6 -36.8 -27.3 ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] 16.5 16.5 16 15.5 15.8 ...
..$ : num [1:241, 1] 19.7 19.6 19.5 19.3 19.2 ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
..$ : num [1:241, 1] 93.6 93.1 93.6 97.2 97.4 ...
..$ : num [1:241, 1] -0.207 -0.831 -0.687 -0.214 -0.152 ...
..$ :List of 15
.. ..$ : chr [1, 1] "decimal day of year"
.. ..$ : chr [1, 1] "net radiation (W/m2)"
.. ..$ : chr [1, 1] "sensible heat flux (W/m2)"
.. ..$ : chr [1, 1] "latent heat flux (W/m2)"
.. ..$ : chr [1, 1] "ground heat flux (W/m2)"
.. ..$ : chr [1, 1] "net ecosystem CO2 exchange (micromol/m2/s)"
.. ..$ : chr [1, 1] "friction velocity (m/s)"
.. ..$ : chr [1, 1] "air temperature (oC)"
.. ..$ : chr [1, 1] "soil temperature at 2 cm (oC)"
.. ..$ : chr [1, 1] "air pressure (kPa)"
.. ..$ : chr [1, 1] "saturation vapor pressure at z = 3m (kPa)"
.. ..$ : chr [1, 1] "actual vapor pressure at z = 3 m (kPa)"
.. ..$ : chr [1, 1] "specific humidity at z = 3 m (g/kg)"
.. ..$ : chr [1, 1] "Relative Humidity at 3 m)"
.. ..$ : chr [1, 1] "PPFD micromol m-2 s-1"
.. ..- attr(*, "dim")= int [1:3] 15 1 1
.. ..- attr(*, "dimnames")=List of 3
.. .. ..$ : chr [1:15] "DDOY" "Rn" "H" "LE" ...
.. .. ..$ : NULL
.. .. ..$ : NULL
..- attr(*, "dim")= int [1:3] 16 1 1
..- attr(*, "dimnames")=List of 3
.. ..$ : chr [1:16] "DDOY" "Rn" "H" "LE" ...
.. ..$ : NULL
.. ..$ : NULL
- attr(*, "header")=List of 3
..$ description: chr "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Tue Nov 28 09:51:53 2006                                                  "
..$ version    : chr "5"
..$ endian    : chr "little"

根据我到目前为止所学到的内容,有16个变量描述了15个数据变量.我可以通过键入以下内容来访问每个单独的变量:

From what I have learned so far, there are 15 data variables which are described by the 16th variable. I can access each individual variable by typing:

mlab$DATA[[1]]
mlab$DATA[[2]]
mlab$DATA[[3]]

从mlab $ DATA [16]中可以看到一年中的小数日",净辐射"和显热通量"的值.我需要做的是将每个变量转换为数据框列,并保留最后一个列表mlab $ DATA [[16]]作为列名.

which shows me the values of 'decimal day of year', 'net radiation', 'and sensible heat flux' -- as seen from mlab$DATA[[16]]. What I need to do is to convert each of those variables to a data frame column, keeping the last list, mlab$DATA[[16]], as the names of the columns.

有人对如何实现这一目标有任何线索吗?非常感谢您的任何指示.

Does anybody have any clues on how to achieve that? Many thanks in advance for any direction.

推荐答案

为什么不只是从该列表对象中提取?

Why not just extract from that list object?

dat <- as.data.frame( mlab$ DATA[1:15]) 
colnames(dat) <- unlist( mlab$ DATA[16] )

(如果您进行移调(?t)并使用带有options(width=150) ...的宽屏,并四舍五入,则显示可能会更好.

(It may display better if you take the transpose ( ?t ) and use a wide screen with options(width=150) ... and use round to 3 places.

round( t(dat) , 3)

这篇关于R-帮助将嵌套列表(?)转换为data.frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆