R - Help converting nested lists(?) to data.frame

独自空忆成欢 提交于 2020-01-04 04:38:09

问题


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)

And this is the structure of the file:

> 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"

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]]

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.


回答1:


Why not just extract from that list object?

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

(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)


来源:https://stackoverflow.com/questions/27306672/r-help-converting-nested-lists-to-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!