映射并套用以调用存储在字符串中的函数

时间:2020-05-09 19:05:59

标签: r

我有一些时间序列数据,我想通过某些功能来lapplymap。例如,我想将funcs中的两个函数应用于数据。

代码:

library(tsfeatures)
funcs = c("ac_9", "unitroot_pp")
map(funcs, ~function(x) tsfeatures(data$close, features = x))

当前有效的是:

tsfeatures(data$close, features = "ac_9")
tsfeatures(data$close, features = "unitroot_pp")

我想将函数名称存储为字符串并调用它。

数据:

data <- structure(c(59.25, 59.25, 59.26, 59.27, 59.26, 59.29, 59.28, 
59.29, 59.31, 59.34, 59.33, 59.34, 59.38, 59.39, 59.35, 59.42, 
59.44, 59.45, 59.46, 59.47, 59.47, 59.48, 59.49, 59.47, 59.51, 
59.51, 59.53, 59.55, 59.6, 59.59, 59.58, 59.6, 59.58, 59.56, 
59.54, 59.51, 59.52, 59.54, 59.6, 59.62, 59.57, 59.57, 59.65, 
59.25, 59.27, 59.3, 59.28, 59.29, 59.3, 59.31, 59.31, 59.35, 
59.35, 59.35, 59.38, 59.39, 59.4, 59.42, 59.46, 59.47, 59.46, 
59.47, 59.49, 59.48, 59.49, 59.5, 59.51, 59.54, 59.52, 59.56, 
59.6, 59.6, 59.62, 59.61, 59.6, 59.63, 59.66, 59.55, 59.53, 59.56, 
59.61, 59.65, 59.62, 59.58, 59.66, 59.71, 59.25, 59.24, 59.26, 
59.26, 59.25, 59.26, 59.27, 59.26, 59.3, 59.31, 59.33, 59.32, 
59.36, 59.34, 59.34, 59.41, 59.41, 59.42, 59.45, 59.46, 59.47, 
59.47, 59.47, 59.47, 59.5, 59.49, 59.52, 59.55, 59.54, 59.57, 
59.57, 59.56, 59.56, 59.53, 59.5, 59.49, 59.51, 59.53, 59.56, 
59.55, 59.55, 59.56, 59.62, 59.25, 59.27, 59.27, 59.27, 59.29, 
59.28, 59.28, 59.31, 59.35, 59.33, 59.33, 59.38, 59.39, 59.34, 
59.42, 59.44, 59.45, 59.46, 59.47, 59.47, 59.48, 59.48, 59.47, 
59.5, 59.51, 59.52, 59.55, 59.6, 59.58, 59.58, 59.61, 59.58, 
59.56, 59.54, 59.51, 59.52, 59.54, 59.61, 59.62, 59.57, 59.57, 
59.66, 59.67), .Dim = c(43L, 4L), .Dimnames = list(NULL, c("open", 
"high", "low", "close")), index = structure(c(1588972675, 1588972680, 
1588972740, 1588972800, 1588972860, 1588972920, 1588972980, 1588973040, 
1588973100, 1588973160, 1588973220, 1588973280, 1588973340, 1588973400, 
1588973460, 1588973520, 1588973580, 1588973640, 1588973700, 1588973760, 
1588973820, 1588973880, 1588973940, 1588974000, 1588974060, 1588974120, 
1588974180, 1588974240, 1588974300, 1588974360, 1588974420, 1588974480, 
1588974540, 1588974600, 1588974660, 1588974720, 1588974780, 1588974840, 
1588974900, 1588974960, 1588975020, 1588975080, 1588975140), tzone = "", tclass = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"))

编辑:

当我通过时:

> newFunction <- function(x){
+   tsfeatures(data$close, features = x)
+ }
> 
> map(funcs, ~newFunction(.x))
[[1]]
# A tibble: 1 x 1
   ac_9
  <dbl>
1 0.385

[[2]]
# A tibble: 1 x 1
  unitroot_pp
        <dbl>
1      -0.899

1 个答案:

答案 0 :(得分:1)

您错误地使用了“ purrr”包中的map

您需要将映射器函数 作为函数或公式传递。您正在尝试传递包含函数的公式。

要解决此问题,请写一个

map(funcs, function(x) tsfeatures(data$close, features = x))

map(funcs, ~ tsfeatures(data$close, features = .x))

与使用lapply时类似,但这仅适用于函数,不适用于公式。