2014-12-05 75 views
2

我需要將.mat(Matlab)數據文件導入到R中,並將其內容組織爲數據框。雖然通過使用R.matlab包直接進行導入,但是轉換爲數據框顯得很難,因爲數據最初是以某種尷尬的方式組織的。它看起來像有兩個嵌套列表。到目前爲止,我還沒有能夠將其轉換爲數據框。R - 幫助將嵌套列表(?)轉換爲數據。框架

這是我到目前爲止有:

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

從我已經學會爲止,有15個數據變量,是由第16個變量描述。我可以通過鍵入訪問每個變量:

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

這說明我是一年中十進制日「,「淨輻射」,「和感熱通量」的價值觀 - 從MLAB $ DATA [見過16]。我需要做的是將每個變量轉換爲數據框列,並將最後一個列表mlab $ DATA [[16]]作爲列的名稱。

有沒有人有關於如何實現這一目標的線索?非常感謝任何方向。

回答

1

爲什麼不從該列表中提取對象?

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

(它可能顯示更好,如果你走轉(?t)並使用寬屏幕options(width=150) ......並採用圓形的3個地方。

round(t(dat) , 3) 
+0

因爲......我沒」 (而且還不是)熟悉列表對象嗎?但是你的答案完成了所有的工作,非常感謝。爲了讓最終結構更加方便,我使用了colnames(dat)< - unlist(dimnames(mlab $ DATA) )[1:15]而不是轉置數據幀。再次感謝您的幫助。 – thiagoveloso 2014-12-05 03:01:39