简体   繁体   中英

R lapply with several dynamic arguments

I'm trying to run an lapply function using inputs from a data frame (example below).

Here below the example of the csv file, which I called holdings.csv :

ticker,buy_date,shares_hold,buy_price,sell_date
"AAPL","2019-11-25",0.07,264.5714,
"MSFT","2019-11-29",0.195,151.85,
"MSFT","2020-04-08",0.6,165.17,
"DIS","2020-01-16",0.15,144.6,
"AMZN","2020-04-08",0.048,2017.5,
"BRK-B","2020-04-09",0.75,195.28,

Then I imported that data into a data frame with:

lots <- read_csv("holdings.csv")

> lots
# A tibble: 6 x 5
  ticker buy_date   shares_hold buy_price sell_date
  <chr>  <date>           <dbl>     <dbl> <lgl>    
1 AAPL   2019-11-25       0.07       265. NA       
2 MSFT   2019-11-29       0.195      152. NA       
3 MSFT   2020-04-08       0.6        165. NA       
4 DIS    2020-01-16       0.15       145. NA       
5 AMZN   2020-04-08       0.048     2018. NA       
6 BRK-B  2020-04-09       0.75       195. NA

Then I run the following code using the tq_get function from the tidyquant package:

prices <- lapply(lots$ticker, FUN = tq_get, 
                          from = lots$buy_date,
                          get = "stock.prices",
                          to = today())

The lapply function gets and uses the first argument ( ticker vector) properly (going row by row in the data frame column defined), but it assumes the second argument ( buy_date vector) as an static value (not going row by row).

Basically, I want to get stock prices from different companies (first argument) using a different start date (second argument), but it looks for the stock prices for all different companies but from the same date.

Here below the output, where there are 6 lists (same as the one for the input data frame) but each list has the same number of rows (because all were gotten from the same starting date).

> prices
[[1]]
# A tibble: 94 x 8
   symbol date        open  high   low close   volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 AAPL   2019-11-25  263.  266.  263.  266. 21005100     266.
 2 AAPL   2019-11-26  267.  267.  262.  264. 26301900     264.
 3 AAPL   2019-11-27  266.  268.  265.  268. 16308900     267.
 4 AAPL   2019-11-29  267.  268   266.  267. 11654400     267.
 5 AAPL   2019-12-02  267.  268.  263.  264. 23621800     264.
 6 AAPL   2019-12-03  258.  260.  256.  259. 28607600     259.
 7 AAPL   2019-12-04  261.  263.  261.  262. 16795400     261.
 8 AAPL   2019-12-05  264.  266.  263.  266. 18606100     265.
 9 AAPL   2019-12-06  267.  271   267.  271. 26518900     270.
10 AAPL   2019-12-09  270   271.  265.  267. 32010600     266.
# ... with 84 more rows

[[2]]
# A tibble: 94 x 8
   symbol date        open  high   low close   volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 MSFT   2019-11-25  150   151.  150.  151. 22420900     151.
 2 MSFT   2019-11-26  151.  152.  151.  152. 24620100     152.
 3 MSFT   2019-11-27  152.  152.  152.  152. 15184400     152.
 4 MSFT   2019-11-29  152.  152.  151.  151. 11977300     151.
 5 MSFT   2019-12-02  152.  152.  148.  150. 27418400     149.
 6 MSFT   2019-12-03  147.  149.  147.  149. 24066000     149.
 7 MSFT   2019-12-04  150.  150.  149.  150. 17574700     149.
 8 MSFT   2019-12-05  150.  150.  149.  150. 17869100     150.
 9 MSFT   2019-12-06  151.  152.  150.  152. 16403500     151.
10 MSFT   2019-12-09  151.  152.  151.  151. 16687400     151.
# ... with 84 more rows

[[3]]
# A tibble: 94 x 8
   symbol date        open  high   low close   volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 MSFT   2019-11-25  150   151.  150.  151. 22420900     151.
 2 MSFT   2019-11-26  151.  152.  151.  152. 24620100     152.
 3 MSFT   2019-11-27  152.  152.  152.  152. 15184400     152.
 4 MSFT   2019-11-29  152.  152.  151.  151. 11977300     151.
 5 MSFT   2019-12-02  152.  152.  148.  150. 27418400     149.
 6 MSFT   2019-12-03  147.  149.  147.  149. 24066000     149.
 7 MSFT   2019-12-04  150.  150.  149.  150. 17574700     149.
 8 MSFT   2019-12-05  150.  150.  149.  150. 17869100     150.
 9 MSFT   2019-12-06  151.  152.  150.  152. 16403500     151.
10 MSFT   2019-12-09  151.  152.  151.  151. 16687400     151.
# ... with 84 more rows

[[4]]
# A tibble: 94 x 8
   symbol date        open  high   low close   volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
 1 DIS    2019-11-25  149.  150.  148.  150. 11316800     149.
 2 DIS    2019-11-26  152.  153.  151.  152. 24949900     151.
 3 DIS    2019-11-27  152.  153.  151.  151.  6155400     151.
 4 DIS    2019-11-29  151.  152.  151.  152.  6284900     151.
 5 DIS    2019-12-02  153.  153.  149.  151. 10351000     150.
 6 DIS    2019-12-03  148.  149.  147.  149.  9273800     148.
 7 DIS    2019-12-04  149.  149.  148.  148.  7684800     147.
 8 DIS    2019-12-05  149.  149.  147.  147.  7363300     147.
 9 DIS    2019-12-06  148.  149.  147.  148.  7084900     147.
10 DIS    2019-12-09  148.  149.  145.  146. 11515000     145.
# ... with 84 more rows

[[5]]
# A tibble: 94 x 8
   symbol date        open  high   low close  volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
 1 AMZN   2019-11-25 1753. 1777. 1753. 1774. 3486200    1774.
 2 AMZN   2019-11-26 1780. 1797. 1778. 1797. 3181200    1797.
 3 AMZN   2019-11-27 1801  1824. 1797. 1819. 3025600    1819.
 4 AMZN   2019-11-29 1818. 1825. 1801. 1801. 1923400    1801.
 5 AMZN   2019-12-02 1804. 1806. 1763. 1782. 3925600    1782.
 6 AMZN   2019-12-03 1760  1773. 1747. 1770. 3380900    1770.
 7 AMZN   2019-12-04 1774. 1789. 1760. 1761. 2670100    1761.
 8 AMZN   2019-12-05 1764. 1764. 1740  1740. 2823800    1740.
 9 AMZN   2019-12-06 1751. 1754. 1740. 1752. 3117400    1752.
10 AMZN   2019-12-09 1751. 1767. 1746. 1750. 2442800    1750.
# ... with 84 more rows

[[6]]
# A tibble: 94 x 8
   symbol date        open  high   low close  volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>
 1 BRK-B  2019-11-25  219.  219.  218.  219. 3648500     219.
 2 BRK-B  2019-11-26  219.  219.  218.  219. 3717800     219.
 3 BRK-B  2019-11-27  219.  221.  218.  220. 3945500     220.
 4 BRK-B  2019-11-29  220.  221.  220.  220. 2256500     220.
 5 BRK-B  2019-12-02  221.  221.  220.  220. 3888100     220.
 6 BRK-B  2019-12-03  219.  219.  216.  218. 4480100     218.
 7 BRK-B  2019-12-04  218.  219.  218.  218. 2667700     218.
 8 BRK-B  2019-12-05  219.  220.  218.  220. 2319300     220.
 9 BRK-B  2019-12-06  222.  223   221.  223. 3447700     223.
10 BRK-B  2019-12-09  223.  223.  221.  221. 2601000     221.
# ... with 84 more rows

I've tried mapply but it doesn't give me the result that I need.

prices <- mapply(lots$ticker, FUN = tq_get, 
                   from = lots$buy_date,
                   get = "stock.prices",
                   to = today())

> prices
         AAPL         MSFT         MSFT        DIS          AMZN        BRK-B      
symbol   Character,94 Character,91 Character,2 Character,59 Character,2 Character,2
date     Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  
open     Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  
high     Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  
low      Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  
close    Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  
volume   Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  
adjusted Numeric,94   Numeric,91   Numeric,2   Numeric,59   Numeric,2   Numeric,2  

Your result from mapply() is because the function tries to convert the output to a matrix with the simplify argument by default. You can get your desired output with the following:

prices <- mapply(lots$ticker, FUN = tq_get, 
                   from = lots$buy_date,
                   get = "stock.prices",
                   to = today(), SIMPLIFY = FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM