【发布时间】:2020-05-17 19:10:46
【问题描述】:
我想根据名为 dfnew1 的旧数据框的条件填充一个名为 hd5 的新数据框。
我可以不使用嵌套的for 循环吗?
for( j in 2 : length(hd6) )
{
for( i in 1: length(hd5$DATE) )
{
abcd= dfnew1 %>%
filter( (Date == hd5$DATE[i]) , (StrikePrice== hd6[j]) , (OptionType== "CE")) %>%
arrange( dte )
hd5[i,j]= abcd[1,9]
}
}
hd6= [13900,14000,14100,14200]
dfnew1 看起来像这样
Date expiry optiontype strikeprice closeprice dte
1/1/2019 31/1/2019 ce 13900 700 30
1/1/2019 31/1/2019 ce 14000 650 30
1/1/2019 31/1/2019 ce 14100 600 30
1/1/2019 31/2/2019 ce 14100 900 58
1/2/2019 31/1/2019 ce 13900 800 29
1/2/2019 31/1/2019 ce 14000 750 29
1/2/2019 31/1/2019 ce 14100 700 29
我想通过处理日期和 strtkeprice 和 optiontype 从这个 dfnew1 数据帧填充我的新数据帧 hd5
我要填充的hd5应该是这样的
Date 13900 14000 14100 14200
1/1/2019 700 650 600 550
1/2/2019 800 750 700 650
【问题讨论】:
-
能否请您添加 (a) 来自
hd5的一些代表性数据 - 比如 5 - 10 行,以及 (b) 您希望hd6看起来像什么? -
是的,但我要求您编辑您的帖子,以便我们可以看到
hd5的样子,以及您想要hd6的样子。 -
你有 3 个数据集,即 hd6、hd5 和 dfnew1
-
是的。我有 3 个数据集。
-
@r2evans 现在正确吗??