[英]R lapply with several dynamic arguments
我正在嘗試使用來自數據幀的輸入(下面的示例)運行lapply
function。
下面是 csv 文件的示例,我將其命名為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,
然后我將該數據導入數據框:
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
然后我使用來自tq_get
package 的tidyquant
function 運行以下代碼:
prices <- lapply(lots$ticker, FUN = tq_get,
from = lots$buy_date,
get = "stock.prices",
to = today())
lapply
function 正確獲取並使用第一個參數( ticker vector)(在定義的數據框列中逐行進行),但它將第二個參數( buy_date向量)假定為 static 值(不是逐行)。
基本上,我想使用不同的開始日期(第二個參數)從不同的公司(第一個參數)獲取股票價格,但它會查找所有不同公司的股票價格,但從同一日期開始。
在 output 下方,這里有 6 個列表(與輸入數據幀的列表相同),但每個列表的行數相同(因為所有列表都是從相同的開始日期獲得的)。
> 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
我試過mapply
,但它沒有給我我需要的結果。
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
您從 mapply() 得到的結果是因為 function 嘗試將 output 默認轉換為帶有簡化參數的矩陣。 您可以通過以下方式獲得所需的 output:
prices <- mapply(lots$ticker, FUN = tq_get,
from = lots$buy_date,
get = "stock.prices",
to = today(), SIMPLIFY = FALSE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.