如何使用应用族而不是为我的问题嵌套for循环

问题描述 投票:0回答:1

我想根据来自旧数据帧hd5的条件来填充新数据帧dfnew1。>

我可以不使用嵌套的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

我想根据旧数据帧dfnew1中的条件填充新数据帧hd5。没有嵌套的for循环,我可以做到吗? for(j in 2:length(hd6)){for(i in 1:...

r for-loop apply
1个回答
0
投票

这里是一个tidyverse选项:


0
投票

我们也可以使用spread

© www.soinside.com 2019 - 2024. All rights reserved.