Make items in list headers in a dataframe

purrr

#1

I am having difficulty getting data to look how I would like after passing to map. Currently, I have all the data I need in a list, but I would like the items in the list to become headers in a nested dataframe.

Below is the reprex

library(tsibble)
library(lubridate)
library(tsfeatures)
library(purrr)

# data prep
weather <- nycflights13::weather %>%
  select(origin, time_hour, temp) 
weather_tsbl <- as_tsibble(weather, key = id(origin), index = time_hour) %>% 
  spread(key = origin, value = temp)

# get tsfeatures
weather_tsbl %>%
  select(-time_hour) %>% 
  map(~ts(.,frequency = 7)) %>%  # pretend data is daily
  map(~tsfeatures::acf_features(.))
#> $EWR
#>      x_acf1     x_acf10  diff1_acf1 diff1_acf10  diff2_acf1 diff2_acf10 
#>   0.9942173   8.6803613   0.4329437   0.6123397  -0.4438008   0.2030792 
#>   seas_acf1 
#>   0.9051399 
#> 
#> $JFK
#>      x_acf1     x_acf10  diff1_acf1 diff1_acf10  diff2_acf1 diff2_acf10 
#>   0.9941960   8.8787780   0.2251941   0.2575175  -0.5206391   0.2810568 
#>   seas_acf1 
#>   0.9204314 
#> 
#> $LGA
#>      x_acf1     x_acf10  diff1_acf1 diff1_acf10  diff2_acf1 diff2_acf10 
#>   0.9962239   9.1287769   0.3364568   0.4056202  -0.4828037   0.2391174 
#>   seas_acf1 
#>   0.9386394 
#> 
#> $time_hour
#>        x_acf1       x_acf10    diff1_acf1   diff1_acf10    diff2_acf1 
#>  9.996557e-01  9.962177e+00 -5.999244e-04  3.369382e-06 -5.000000e-01 
#>   diff2_acf10     seas_acf1 
#>  2.500000e-01  9.975902e-01

# make data look like this:
 
#  origin      data                x_acf1      x_acf10     diff1_acf1  diff1_acf10  diff2_acf1  diff2_acf10  seas_acf1
#  <chr>       <list>              
#  1 EWR    <tibble [8,708 x 2]>   0.9942173   8.6803613   0.4329437   0.6123397    -0.4438008   0.2030792   0.9051399 
#  2 JFK    <tibble [8,711 x 2]>   0.9941960   8.8787780   0.2251941   0.2575175    -0.5206391   0.2810568   0.9204314 
#  3 LGA    <tibble [8,711 x 2]>   0.9962239   9.1287769   0.3364568   0.4056202    -0.4828037   0.2391174   0.9386394 

Created on 2018-09-08 by the reprex package (v0.2.0).

The above code gives me all of the data I need, but I would like it in the following format.

# make data look like this:
 
#  origin      data                x_acf1      x_acf10     diff1_acf1  diff1_acf10  diff2_acf1  diff2_acf10  seas_acf1
#  <chr>       <list>              
#  1 EWR    <tibble [8,708 x 2]>   0.9942173   8.6803613   0.4329437   0.6123397    -0.4438008   0.2030792   0.9051399 
#  2 JFK    <tibble [8,711 x 2]>   0.9941960   8.8787780   0.2251941   0.2575175    -0.5206391   0.2810568   0.9204314 
#  3 LGA    <tibble [8,711 x 2]>   0.9962239   9.1287769   0.3364568   0.4056202    -0.4828037   0.2391174   0.9386394 

I know how to nest the data and join, but I don't know how to make the names, such as x_acf1, headers in a dataframe.


#2

It's not obvious to me what is in the data column, but here is how to get the rest:

weather_tsbl %>%
    as_tibble() %>%
    select(-time_hour) %>% 
    map(~ts(.,frequency = 7)) %>%  # pretend data is daily
    map(~ tsfeatures::acf_features(.)) %>%
    imap_dfr(~ c(list(origin = ..2), as.list(..1)))

# # A tibble: 3 x 8
#   origin x_acf1 x_acf10 diff1_acf1 diff1_acf10 diff2_acf1 diff2_acf10 seas_acf1
#   <chr>   <dbl>   <dbl>      <dbl>       <dbl>      <dbl>       <dbl>     <dbl>
# 1 EWR     0.994    8.69      0.455       0.661     -0.434       0.195     0.905
# 2 JFK     0.994    8.88      0.234       0.268     -0.520       0.280     0.920
# 3 LGA     0.996    9.13      0.365       0.445     -0.465       0.222     0.939
  • as_tibble: added to be able to remove time_hour which is an index
  • imap_dfr: converts the vector from acf_features to a list, adds the origin field (from the names of the list), and binds (by row) the lists everything into a data frame

#3

Very impressive one line of code. Thanks!