1
List of 29
$ : num [1:2, 1:7] -0.424 1.84 4.125 1.84 2.935 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:2] "Training set" "Test set"

result[[1]]
                 ME     RMSE      MAE       MPE     MAPE      MASE
Training set -0.4238181 4.124630 2.934922 -5.749091 15.25211 0.8298791
Test set      1.8400343 1.840034 1.840034 10.300856 10.30086 0.5202885
               ACF1
Training set -0.0218282
Test set             NA

> dput(result)
list(structure(c(0.513396419391301, -37.8812034057995, 18.8299932348763, 
37.8812034057995, 15.9937453259579, 37.8812034057995, -11.1574789625766, 
-184.084337396461, 29.1724105151215, 184.084337396461, 0.817172411862546, 
1.93547375680242, 0.0818136754061593, NA), .Dim = c(2L, 7L), .Dimnames = list(
c("Training set", "Test set"), c("ME", "RMSE", "MAE", "MPE", 
"MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-22.319971645238, 18.8299932348763, 22.319971645238, 15.9937453259579, 
22.319971645238, -11.1574789625766, -61.7607524011407, 29.1724105151215, 
61.7607524011407, 0.817172411862546, 1.14039986821851, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
27.1904085572912, 18.8299932348763, 27.1904085572912, 15.9937453259579, 
27.1904085572912, -11.1574789625766, 31.7460304729518, 29.1724105151215, 
31.7460304729518, 0.817172411862546, 1.38924631394672, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), 
structure(c(0.513396419391301, 
    53.1089634908355, 18.8299932348763, 53.1089634908355, 15.9937453259579, 
53.1089634908355, -11.1574789625766, 47.6021790380844, 29.1724105151215, 
47.6021790380844, 0.817172411862546, 2.71350949404508, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
14.6193112181759, 18.8299932348763, 14.6193112181759, 15.9937453259579, 
14.6193112181759, -11.1574789625766, 20.0048887135367, 29.1724105151215, 
20.0048887135367, 0.817172411862546, 0.746948107804167, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-1.06040238179536, 18.8299932348763, 1.06040238179536, 15.9937453259579, 
1.06040238179536, -11.1574789625766, -1.84742375550171, 29.1724105151215, 
1.84742375550171, 0.817172411862546, 0.0541794028988392, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
50.7310943575706, 18.8299932348763, 50.7310943575706, 15.9937453259579, 
50.7310943575706, -11.1574789625766, 46.4610981905357, 29.1724105151215, 
46.4610981905357, 0.817172411862546, 2.59201643440696, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
55.0138373829973, 18.8299932348763, 55.0138373829973, 15.9937453259579, 
55.0138373829973, -11.1574789625766, 48.4817807788847, 29.1724105151215, 
48.4817807788847, 0.817172411862546, 2.81083569006907, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
27.3624500366051, 18.8299932348763, 27.3624500366051, 15.9937453259579, 
27.3624500366051, -11.1574789625766, 31.8828547941145, 29.1724105151215, 
31.8828547941145, 0.817172411862546, 1.39803647208205, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
4.75759494704218, 18.8299932348763, 4.75759494704218, 15.9937453259579, 
4.75759494704218, -11.1574789625766, 7.52581852119078, 29.1724105151215, 
7.52581852119078, 0.817172411862546, 0.243080983115921, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-21.5672042430535, 18.8299932348763, 21.5672042430535, 15.9937453259579, 
21.5672042430535, -11.1574789625766, -58.460100899509, 29.1724105151215, 
58.460100899509, 0.817172411862546, 1.10193853592405, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-22.7300084731155, 18.8299932348763, 22.7300084731155, 15.9937453259579, 
22.7300084731155, -11.1574789625766, -63.6171484011973, 29.1724105151215, 
63.6171484011973, 0.817172411862546, 1.16134998195111, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-6.93434513416145, 18.8299932348763, 6.93434513416145, 15.9937453259579, 
6.93434513416145, -11.1574789625766, -13.4582051337587, 29.1724105151215, 
13.4582051337587, 0.817172411862546, 0.354298222366539, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
26.5787861060258, 18.8299932348763, 26.5787861060258, 15.9937453259579, 
26.5787861060258, -11.1574789625766, 31.2551254927233, 29.1724105151215, 
31.2551254927233, 0.817172411862546, 1.35799653577008, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
36.5896815034013, 18.8299932348763, 36.5896815034013, 15.9937453259579, 
36.5896815034013, -11.1574789625766, 38.4955729378816, 29.1724105151215, 
38.4955729378816, 0.817172411862546, 1.86948570669616, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
18.3502949767713, 18.8299932348763, 18.3502949767713, 15.9937453259579, 
18.3502949767713, -11.1574789625766, 23.890603032012, 29.1724105151215, 
23.890603032012, 0.817172411862546, 0.937576189875915, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-6.51749862530565, 18.8299932348763, 6.51749862530565, 15.9937453259579, 
6.51749862530565, -11.1574789625766, -12.547674868192, 29.1724105151215, 
12.547674868192, 0.817172411862546, 0.333000179908321, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
20.0687421145515, 18.8299932348763, 20.0687421145515, 15.9937453259579, 
20.0687421145515, -11.1574789625766, 25.5561206682837, 29.1724105151215, 
25.5561206682837, 0.817172411862546, 1.02537723732407, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
6.01122360728164, 18.8299932348763, 6.01122360728164, 15.9937453259579, 
6.01122360728164, -11.1574789625766, 9.32397573473786, 29.1724105151215, 
9.32397573473786, 0.817172411862546, 0.307132944366376, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
12.7182212474731, 18.8299932348763, 12.7182212474731, 15.9937453259579, 
12.7182212474731, -11.1574789625766, 17.8682910031556, 29.1724105151215, 
17.8682910031556, 0.817172411862546, 0.64981524462136, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
6.99715747218507, 18.8299932348763, 6.99715747218507, 15.9937453259579, 
6.99715747218507, -11.1574789625766, 10.6897760658741, 29.1724105151215, 
10.6897760658741, 0.817172411862546, 0.357507508791413, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
14.8697440743207, 18.8299932348763, 14.8697440743207, 15.9937453259579, 
14.8697440743207, -11.1574789625766, 20.2780871725288, 29.1724105151215, 
20.2780871725288, 0.817172411862546, 0.759743536072824, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
11.879111326578, 18.8299932348763, 11.879111326578, 15.9937453259579, 
11.879111326578, -11.1574789625766, 16.8884927626238, 29.1724105151215, 
16.8884927626238, 0.817172411862546, 0.606942392521939, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-8.12047355873076, 18.8299932348763, 8.12047355873076, 15.9937453259579, 
8.12047355873076, -11.1574789625766, -16.1316044330997, 29.1724105151215, 
16.1316044330997, 0.817172411862546, 0.414901377270531, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
-12.5107370397529, 18.8299932348763, 12.5107370397529, 15.9937453259579, 
12.5107370397529, -11.1574789625766, -27.227651862362, 29.1724105151215, 
27.227651862362, 0.817172411862546, 0.63921420233948, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
8.10859725045342, 18.8299932348763, 8.10859725045342, 15.9937453259579, 
8.10859725045342, -11.1574789625766, 12.1809274700972, 29.1724105151215, 
12.1809274700972, 0.817172411862546, 0.414294578094901, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
8.74673451746508, 18.8299932348763, 8.74673451746508, 15.9937453259579, 
8.74673451746508, -11.1574789625766, 13.0147894156911, 29.1724105151215, 
13.0147894156911, 0.817172411862546, 0.446899084353792, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
28.2300570740269, 18.8299932348763, 28.2300570740269, 15.9937453259579, 
28.2300570740269, -11.1574789625766, 32.5645862171677, 29.1724105151215, 
32.5645862171677, 0.817172411862546, 1.44236533445103, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))), structure(c(0.513396419391301, 
14.0381287176184, 18.8299932348763, 14.0381287176184, 15.9937453259579, 
14.0381287176184, -11.1574789625766, 19.3636010547358, 29.1724105151215, 
19.3636010547358, 0.817172411862546, 0.717253605607607, 0.0818136754061593, 
NA), .Dim = c(2L, 7L), .Dimnames = list(c("Training set", "Test set"
), c("ME", "RMSE", "MAE", "MPE", "MAPE", "MASE", "ACF1"))))

I want to extract values of Training set and Test set from a list into a data frame while preserving the column names as shown.

It could be Training set and Test set could be column names and other variables as rows of the Train and Test like long format in R, e.g ME, RMSE, MAE, MPE, MAPE, etc.

alistaire
  • 42,459
  • 4
  • 77
  • 117
user3459293
  • 320
  • 1
  • 3
  • 11
  • 1
    `data.frame(t(result$num))`? You need to edit with your data presented in the format returned by `dput(result)` so [your example is reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610). – alistaire Jun 27 '17 at 14:33
  • thanks for reply. reply have a look I changed the profile – user3459293 Jun 27 '17 at 16:56

1 Answers1

0

There are different ways to do this, but purrr (part of the tidyverse) is handy:

library(tidyverse)

df_result <- result %>% 
    map(as.data.frame) %>%    # convert each matrix to data.frame
    # add rownames of data.frames as column; simplify all to one data.frame 
    # with an ID column with which list element it came from
    map_df(rownames_to_column, 'subset', .id = 'element') %>% 
    tbl_df()    # for pretty printing

df_result
#> # A tibble: 58 x 9
#>    element       subset          ME     RMSE      MAE        MPE      MAPE      MASE       ACF1
#>      <chr>        <chr>       <dbl>    <dbl>    <dbl>      <dbl>     <dbl>     <dbl>      <dbl>
#>  1       1 Training set   0.5133964 18.82999 15.99375  -11.15748  29.17241 0.8171724 0.08181368
#>  2       1     Test set -37.8812034 37.88120 37.88120 -184.08434 184.08434 1.9354738         NA
#>  3       2 Training set   0.5133964 18.82999 15.99375  -11.15748  29.17241 0.8171724 0.08181368
#>  4       2     Test set -22.3199716 22.31997 22.31997  -61.76075  61.76075 1.1403999         NA
#>  5       3 Training set   0.5133964 18.82999 15.99375  -11.15748  29.17241 0.8171724 0.08181368
#>  6       3     Test set  27.1904086 27.19041 27.19041   31.74603  31.74603 1.3892463         NA
#>  7       4 Training set   0.5133964 18.82999 15.99375  -11.15748  29.17241 0.8171724 0.08181368
#>  8       4     Test set  53.1089635 53.10896 53.10896   47.60218  47.60218 2.7135095         NA
#>  9       5 Training set   0.5133964 18.82999 15.99375  -11.15748  29.17241 0.8171724 0.08181368
#> 10       5     Test set  14.6193112 14.61931 14.61931   20.00489  20.00489 0.7469481         NA
#> # ... with 48 more rows

If you want the data in long form, transpose:

df_t_result <- result %>% 
    map(t) %>%    # transpose each matrix
    map(as.data.frame) %>% 
    map_df(rownames_to_column, 'stat', .id = 'element') %>% 
    set_names(~gsub('\\s', '_', .x)) %>%    # make names syntactic
    tbl_df()

df_t_result
#> # A tibble: 203 x 4
#>    element  stat   Training_set    Test_set
#>      <chr> <chr>          <dbl>       <dbl>
#>  1       1    ME     0.51339642  -37.881203
#>  2       1  RMSE    18.82999323   37.881203
#>  3       1   MAE    15.99374533   37.881203
#>  4       1   MPE   -11.15747896 -184.084337
#>  5       1  MAPE    29.17241052  184.084337
#>  6       1  MASE     0.81717241    1.935474
#>  7       1  ACF1     0.08181368          NA
#>  8       2    ME     0.51339642  -22.319972
#>  9       2  RMSE    18.82999323   22.319972
#> 10       2   MAE    15.99374533   22.319972
#> # ... with 193 more rows
alistaire
  • 42,459
  • 4
  • 77
  • 117
  • Hi there, Sorry to bother you. Even though you have example output for the data in long form. however I get error this error "Error: `x` and `nm` must be the same length". thanks in advance – user3459293 Jun 28 '17 at 16:52
  • Oh, that's from `purrr::set_names`. At some point it was updated to accept functions, but I'm not sure if that's on the CRAN version yet. You can update to [the devel version](https://github.com/tidyverse/purrr/), or replace it with an explicit call, e.g. `set_names(gsub('\\s', '_', names(.)))` (which uses `setNames` syntax). Or just omit it and fix them later. – alistaire Jun 28 '17 at 17:02