数据框列表上的映射功能

时间:2019-10-19 19:49:10

标签: r purrr

我正在尝试对数据应用一系列功能,但是我出了点问题。

这些函数可以创建多个列,因此我使用bind_rows将它们添加到原始数据中。

我想做的是将functionsmap中的两个函数放在一个列表上,以在每个列表中创建新列,我想使用mutatesummarise

library(tsfeatures)
library(dplyr)
library(purrr)

functions <- c("stl_features", "max_kl_shift")

Data %>% 
  map(., ~ map(., ~ data.frame(
    bind_cols(
      tsfeatures(.x["Value"], functions)
    )
  )
  )
  ) 

错误:

  

大约(idx,x [idx],tt,rule = 2)中的错误:至少需要两个   要插入的非NA值另外:警告消息:1:输入   min(x):没有min不可缺少的参数;返回Inf 2:在max(x)中:   没有max的必填项;返回-Inf

数据:

Data <- list(structure(list(time = structure(c(17045, 17046, 17050, 17051, 
17052, 17053, 17056, 17057, 17058, 17059, 17060, 17063, 17064, 
17065, 17066, 17067, 17070, 17071, 17072, 17073), class = "Date"), 
    ID = c("CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"), Value = c(0, 0.00348603358425681, 
    0.011173612052706, 0.000346065780582494, -0.00644578606355972, 
    -0.0201981554179086, 0.0123213639426545, -0.0121323473477323, 
    0.00368569810400099, 0.0121575628815795, -0.00373173650186931, 
    -0.00413587683295258, 0.00745717762898512, 0.00623533292069589, 
    0.0141584233987713, -0.000393793258897213, -0.016126574676531, 
    0.0113664093074735, -0.00185184350325229, -0.00838065921587761
    ), out = c(0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 
    0, 1, 1, 0)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(time = structure(c(17056, 
17057, 17058, 17059, 17060, 17063, 17064, 17065, 17066, 17067, 
17070, 17071, 17072, 17073, 17074, 17077, 17078, 17079, 17080, 
17081), class = "Date"), ID = c("CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"
), Value = c(0.0123213639426545, -0.0121323473477323, 0.00368569810400099, 
0.0121575628815795, -0.00373173650186931, -0.00413587683295258, 
0.00745717762898512, 0.00623533292069589, 0.0141584233987713, 
-0.000393793258897213, -0.016126574676531, 0.0113664093074735, 
-0.00185184350325229, -0.00838065921587761, 0.00294185619615428, 
-0.0060852193311054, 0.00500931320547093, 0.0000514895101431101, 
0.000502291156859291, -0.00229123398600595), out = c(1, 0, 1, 
1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    time = structure(c(17064, 17065, 17066, 17067, 17070, 17071, 
    17072, 17073, 17074, 17077, 17078, 17079, 17080, 17081, 17084, 
    17085, 17086, 17087, 17088, 17091), class = "Date"), ID = c("CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1"), Value = c(0.00745717762898512, 0.00623533292069589, 
    0.0141584233987713, -0.000393793258897213, -0.016126574676531, 
    0.0113664093074735, -0.00185184350325229, -0.00838065921587761, 
    0.00294185619615428, -0.0060852193311054, 0.00500931320547093, 
    0.0000514895101431101, 0.000502291156859291, -0.00229123398600595, 
    0.0140114372217135, -0.00365167187405735, 0.00392047706151, 
    -0.0101127189155992, 0.000436945988930848, 0.00183678592569736
    ), out = c(1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 
    1, 0, 0, 1)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(time = structure(c(17072, 
17073, 17074, 17077, 17078, 17079, 17080, 17081, 17084, 17085, 
17086, 17087, 17088, 17091, 17092, 17093, 17094, 17095, 17098, 
17099), class = "Date"), ID = c("CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"
), Value = c(-0.00185184350325229, -0.00838065921587761, 0.00294185619615428, 
-0.0060852193311054, 0.00500931320547093, 0.0000514895101431101, 
0.000502291156859291, -0.00229123398600595, 0.0140114372217135, 
-0.00365167187405735, 0.00392047706151, -0.0101127189155992, 
0.000436945988930848, 0.00183678592569736, 0.0196163746454174, 
0.00784647778278202, -0.00565193886462889, 0.00301143592272179, 
0.0171885235697395, -0.00669036428079295), out = c(1, 0, 1, 0, 
1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    time = structure(c(17080, 17081, 17084, 17085, 17086, 17087, 
    17088, 17091, 17092, 17093, 17094, 17095, 17098, 17099, 17100, 
    17101, 17102, 17105, 17106, 17107), class = "Date"), ID = c("CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1"), Value = c(0.000502291156859291, 
    -0.00229123398600595, 0.0140114372217135, -0.00365167187405735, 
    0.00392047706151, -0.0101127189155992, 0.000436945988930848, 
    0.00183678592569736, 0.0196163746454174, 0.00784647778278202, 
    -0.00565193886462889, 0.00301143592272179, 0.0171885235697395, 
    -0.00669036428079295, -0.0106478836418512, -0.00465545067066953, 
    0.0000251700516804565, -0.0136163258207899, -0.00118539912060411, 
    -0.0190272881732103), out = c(0, 0, 1, 0, 1, 0, 0, 1, 1, 
    1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0)), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    time = structure(c(17088, 17091, 17092, 17093, 17094, 17095, 
    17098, 17099, 17100, 17101, 17102, 17105, 17106, 17107, 17108, 
    17109, 17112, 17113, 17114, 17115), class = "Date"), ID = c("CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1"), Value = c(0.000436945988930848, 
    0.00183678592569736, 0.0196163746454174, 0.00784647778278202, 
    -0.00565193886462889, 0.00301143592272179, 0.0171885235697395, 
    -0.00669036428079295, -0.0106478836418512, -0.00465545067066953, 
    0.0000251700516804565, -0.0136163258207899, -0.00118539912060411, 
    -0.0190272881732103, -0.00854690633203736, -0.000144312649125955, 
    0.0269021803390415, 0.0102105886057713, -0.00657804700031572, 
    -0.0289694516279417), out = c(0, 1, 1, 1, 0, 1, 1, 0, 0, 
    0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0)), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    time = structure(c(17098, 17099, 17100, 17101, 17102, 17105, 
    17106, 17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116, 
    17119, 17120, 17121, 17122, 17123), class = "Date"), ID = c("CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1"), Value = c(0.0171885235697395, -0.00669036428079295, 
    -0.0106478836418512, -0.00465545067066953, 0.0000251700516804565, 
    -0.0136163258207899, -0.00118539912060411, -0.0190272881732103, 
    -0.00854690633203736, -0.000144312649125955, 0.0269021803390415, 
    0.0102105886057713, -0.00657804700031572, -0.0289694516279417, 
    -0.0111990899370517, -0.0237924756958046, 0.0304450229355975, 
    0.00789725649510542, 0.0088295314155904, -0.0138609782778413
    ), out = c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 
    1, 1, 1, 0)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(time = structure(c(17106, 
17107, 17108, 17109, 17112, 17113, 17114, 17115, 17116, 17119, 
17120, 17121, 17122, 17123, 17126, 17127, 17128, 17130, 17133, 
17134), class = "Date"), ID = c("CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"
), Value = c(-0.00118539912060411, -0.0190272881732103, -0.00854690633203736, 
-0.000144312649125955, 0.0269021803390415, 0.0102105886057713, 
-0.00657804700031572, -0.0289694516279417, -0.0111990899370517, 
-0.0237924756958046, 0.0304450229355975, 0.00789725649510542, 
0.0088295314155904, -0.0138609782778413, 0.0113866913646978, 
-0.0012090379426567, -0.00947587412040363, 0.00090671757719174, 
0.00861253683999563, 0.00338440726054889), out = c(1, 0, 0, 1, 
1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    time = structure(c(17114, 17115, 17116, 17119, 17120, 17121, 
    17122, 17123, 17126, 17127, 17128, 17130, 17133, 17134, 17135, 
    17136, 17137, 17140, 17141, 17142), class = "Date"), ID = c("CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
    "CAT1", "CAT1", "CAT1"), Value = c(-0.00657804700031572, 
    -0.0289694516279417, -0.0111990899370517, -0.0237924756958046, 
    0.0304450229355975, 0.00789725649510542, 0.0088295314155904, 
    -0.0138609782778413, 0.0113866913646978, -0.0012090379426567, 
    -0.00947587412040363, 0.00090671757719174, 0.00861253683999563, 
    0.00338440726054889, -0.016605324777718, -0.0133502127773003, 
    0.00344958960669994, 0.0160160159893405, -0.00447205963195563, 
    0.0159133949476373), out = c(1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 
    0, 0, 1, 0, 0, 0, 1, 1, 0, 1)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(time = structure(c(17122, 
17123, 17126, 17127, 17128, 17130, 17133, 17134, 17135, 17136, 
17137, 17140, 17141, 17142, 17143, 17144, 17147, 17148, 17149, 
17150), class = "Date"), ID = c("CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", 
"CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1", "CAT1"
), Value = c(0.0088295314155904, -0.0138609782778413, 0.0113866913646978, 
-0.0012090379426567, -0.00947587412040363, 0.00090671757719174, 
0.00861253683999563, 0.00338440726054889, -0.016605324777718, 
-0.0133502127773003, 0.00344958960669994, 0.0160160159893405, 
-0.00447205963195563, 0.0159133949476373, 0.00678170228664343, 
0.0165760738798502, -0.0000252860172512692, 0.00865350998635406, 
0.00121847887105075, 0.000978545163097477), out = c(1, 0, 1, 
0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame")))

1 个答案:

答案 0 :(得分:1)

示例列表Data中的数据帧仅包含两行,这对于tsfeatures函数而言太小了。另一个问题是当您进行.x["Value"]输出时是一个数据帧,但是tsfeatures函数的文档说第一个参数应该是单变量时间序列对象或向量的列表。因此,我假设您应使用的代码为.x[["Value"]],这将导致一个向量。

我通过将Data中的所有数据帧组合到一个数据帧中,尝试了以下示例。

tsfeatures(bind_rows(Data)[["Value"]], functions)

这将导致以下输出。

# # A tibble: 1 x 10
#     nperiods seasonal_period trend      spike linearity curvature  e_acf1 e_acf10 max_kl_shift time_kl_shift
#        <dbl>           <dbl> <dbl>      <dbl>     <dbl>     <dbl>   <dbl>   <dbl>        <dbl>         <dbl>
#   1        0               1 0.965 0.00000631      2.99     -2.64 -0.0723   0.381           NA            NA

因此,假设Data列表中的数据帧有两行以上。我们可以将以上代码应用于您的每个数据框。我们可以使用map函数包装上面的代码,如下所示。

Data %>% map(., ~tsfeatures(.x[["Value"]], functions))

我认为这可能会起作用。