映射列表并将变量插入函数
map over list and insert variables into a function
我有一个列表,我想遍历并将变量插入函数中。但是,我正在使用的函数不喜欢我从应用 {purr} 包中的 map() 函数得到的输出。
这是我的清单:
$AAPL
# A tibble: 10 x 2
ticker string
<chr> <date>
1 AAPL 2020-01-28
2 AAPL 2020-04-30
3 AAPL 2020-07-30
4 AAPL 2020-10-29
5 AAPL 2021-01-27
6 AAPL 2020-01-29
7 AAPL 2020-05-01
8 AAPL 2020-07-31
9 AAPL 2020-10-30
10 AAPL 2021-01-28
$ABEV
# A tibble: 8 x 2
ticker string
<chr> <date>
1 ABEV 2020-02-26
2 ABEV 2020-05-06
3 ABEV 2020-07-29
4 ABEV 2020-10-28
5 ABEV 2020-02-27
6 ABEV 2020-05-07
7 ABEV 2020-07-30
8 ABEV 2020-10-29
my.list = list(AAPL = structure(list(ticker = c("AAPL", "AAPL", "AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL"), string = structure(c(18289,
18382, 18473, 18564, 18654, 18290, 18383, 18474, 18565, 18655
), class = "Date")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"), na.action = structure(305:380, .Names = c("305",
"306", "307", "308", "309", "310", "311", "312", "313", "314",
"315", "316", "317", "318", "319", "320", "321", "322", "323",
"324", "325", "326", "327", "328", "329", "330", "331", "332",
"333", "334", "335", "336", "337", "338", "339", "340", "341",
"342", "343", "344", "345", "346", "347", "348", "349", "350",
"351", "352", "353", "354", "355", "356", "357", "358", "359",
"360", "361", "362", "363", "364", "365", "366", "367", "368",
"369", "370", "371", "372", "373", "374", "375", "376", "377",
"378", "379", "380"), class = "omit")), ABEV = structure(list(
ticker = c("ABEV", "ABEV", "ABEV", "ABEV", "ABEV", "ABEV",
"ABEV", "ABEV"), string = structure(c(18318, 18388, 18472,
18563, 18319, 18389, 18473, 18564), class = "Date")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(305:380, .Names = c("305",
"306", "307", "308", "309", "310", "311", "312", "313", "314",
"315", "316", "317", "318", "319", "320", "321", "322", "323",
"324", "325", "326", "327", "328", "329", "330", "331", "332",
"333", "334", "335", "336", "337", "338", "339", "340", "341",
"342", "343", "344", "345", "346", "347", "348", "349", "350",
"351", "352", "353", "354", "355", "356", "357", "358", "359",
"360", "361", "362", "363", "364", "365", "366", "367", "368",
"369", "370", "371", "372", "373", "374", "375", "376", "377",
"378", "379", "380"), class = "omit")))
我想遍历它并将变量添加到 Quanld 函数中。 Quandl 函数适用于以下输入。
Quandl.datatable('ORATS/VOL', tradedate=c('2021-02-19', "2020-01-20"), ticker='AAPL')
所以我想做的是遍历列表并将日期 (string) 和代码 (ticker) 插入此函数。
这是我的资料:
library(tidyverse)
map(my.list, ~Quandl.datatable('ORATS/VOL', tradedate=.x$string, ticker=.x$ticker[1]))
这会出错,因为在输入函数时格式看起来不在向量中。我在这里错过了什么?谢谢你的帮助。
如果我们查看手动输入的tradedate
值,它是character
class,而'string'列是Date
class。也许,我们可以将其更改为 character
和 as.character
library(purrr)
out <- map(my.list, ~Quandl.datatable('ORATS/VOL',
tradedate = as.character(.x$string), ticker=.x$ticker[1]))
原因可能是在 API 调用中,它无论如何都会转换为 character
,但是如果我们使用 Date
class,则强制转换为 integer
存储值可能会阻止它执行
我有一个列表,我想遍历并将变量插入函数中。但是,我正在使用的函数不喜欢我从应用 {purr} 包中的 map() 函数得到的输出。
这是我的清单:
$AAPL
# A tibble: 10 x 2
ticker string
<chr> <date>
1 AAPL 2020-01-28
2 AAPL 2020-04-30
3 AAPL 2020-07-30
4 AAPL 2020-10-29
5 AAPL 2021-01-27
6 AAPL 2020-01-29
7 AAPL 2020-05-01
8 AAPL 2020-07-31
9 AAPL 2020-10-30
10 AAPL 2021-01-28
$ABEV
# A tibble: 8 x 2
ticker string
<chr> <date>
1 ABEV 2020-02-26
2 ABEV 2020-05-06
3 ABEV 2020-07-29
4 ABEV 2020-10-28
5 ABEV 2020-02-27
6 ABEV 2020-05-07
7 ABEV 2020-07-30
8 ABEV 2020-10-29
my.list = list(AAPL = structure(list(ticker = c("AAPL", "AAPL", "AAPL",
"AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL", "AAPL"), string = structure(c(18289,
18382, 18473, 18564, 18654, 18290, 18383, 18474, 18565, 18655
), class = "Date")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"), na.action = structure(305:380, .Names = c("305",
"306", "307", "308", "309", "310", "311", "312", "313", "314",
"315", "316", "317", "318", "319", "320", "321", "322", "323",
"324", "325", "326", "327", "328", "329", "330", "331", "332",
"333", "334", "335", "336", "337", "338", "339", "340", "341",
"342", "343", "344", "345", "346", "347", "348", "349", "350",
"351", "352", "353", "354", "355", "356", "357", "358", "359",
"360", "361", "362", "363", "364", "365", "366", "367", "368",
"369", "370", "371", "372", "373", "374", "375", "376", "377",
"378", "379", "380"), class = "omit")), ABEV = structure(list(
ticker = c("ABEV", "ABEV", "ABEV", "ABEV", "ABEV", "ABEV",
"ABEV", "ABEV"), string = structure(c(18318, 18388, 18472,
18563, 18319, 18389, 18473, 18564), class = "Date")), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"), na.action = structure(305:380, .Names = c("305",
"306", "307", "308", "309", "310", "311", "312", "313", "314",
"315", "316", "317", "318", "319", "320", "321", "322", "323",
"324", "325", "326", "327", "328", "329", "330", "331", "332",
"333", "334", "335", "336", "337", "338", "339", "340", "341",
"342", "343", "344", "345", "346", "347", "348", "349", "350",
"351", "352", "353", "354", "355", "356", "357", "358", "359",
"360", "361", "362", "363", "364", "365", "366", "367", "368",
"369", "370", "371", "372", "373", "374", "375", "376", "377",
"378", "379", "380"), class = "omit")))
我想遍历它并将变量添加到 Quanld 函数中。 Quandl 函数适用于以下输入。
Quandl.datatable('ORATS/VOL', tradedate=c('2021-02-19', "2020-01-20"), ticker='AAPL')
所以我想做的是遍历列表并将日期 (string) 和代码 (ticker) 插入此函数。
这是我的资料:
library(tidyverse)
map(my.list, ~Quandl.datatable('ORATS/VOL', tradedate=.x$string, ticker=.x$ticker[1]))
这会出错,因为在输入函数时格式看起来不在向量中。我在这里错过了什么?谢谢你的帮助。
如果我们查看手动输入的tradedate
值,它是character
class,而'string'列是Date
class。也许,我们可以将其更改为 character
和 as.character
library(purrr)
out <- map(my.list, ~Quandl.datatable('ORATS/VOL',
tradedate = as.character(.x$string), ticker=.x$ticker[1]))
原因可能是在 API 调用中,它无论如何都会转换为 character
,但是如果我们使用 Date
class,则强制转换为 integer
存储值可能会阻止它执行