Jordan Wrong
Jordan Wrong

Reputation: 1245

map over list and insert variables into a function

I have a list that I want to loop over and insert the variables into a function. However, the function I am using does not like the outputs I am getting from applying the map() function from {purr} package.

Here is my list:

$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")))

I want to loop over this and add the variables into a Quanld fucntion. The Quandl function works with the following inputs.

Quandl.datatable('ORATS/VOL', tradedate=c('2021-02-19', "2020-01-20"), ticker='AAPL')

So what I am trying to do is loop over the list and insert the dates (string) and the ticker (ticker) into this function.

Here is what I have:

library(tidyverse)

map(my.list, ~Quandl.datatable('ORATS/VOL', tradedate=.x$string, ticker=.x$ticker[1]))

This gives an error because it looks like the format is not in a vector when being input into the function. What am I missing here? Thank you for your help.

Upvotes: 2

Views: 76

Answers (1)

akrun
akrun

Reputation: 887173

If we look at how the tradedate values in the manual entry, it is character class, while the 'string' column is Date class. May be, we can change it to character with as.character

library(purrr)
out <- map(my.list, ~Quandl.datatable('ORATS/VOL', 
     tradedate = as.character(.x$string), ticker=.x$ticker[1]))

The reason could be that in the API call, it is converting to character anyway, but if we use a Date class, the coercion to integer storage values may prevent it from executing

Upvotes: 2

Related Questions