首页 > 解决方案 > 如何为我的问题使用应用系列而不是嵌套的 for 循环

问题描述

我想填充一个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

标签: rfor-loopapply

解决方案


这是一个 tidyverse 选项:

library(dplyr)
# library(tidyr)
dat %>%
  group_by(Date, strikeprice) %>%
  summarize(closeprice = min(closeprice)) %>%
  ungroup() %>%
  tidyr::pivot_wider(names_from = "strikeprice", values_from = "closeprice")
# # A tibble: 2 x 4
#   Date     `13900` `14000` `14100`
#   <chr>      <int>   <int>   <int>
# 1 1/1/2019     700     650     600
# 2 1/2/2019     800     750     700

(您可能会看到在线教程引用tidyr::spread。它在这里有效地做同样的事情,但已经退役(来源:https ://tidyr.tidyverse.org/reference/spread.html ,以及tidyr::gather),因此通常建议新代码应该使用这些pivot_*函数。)

注意:根据您的预期输出,看起来您使用了最小值

1/1/2019  31/1/2019  ce          14100      600        30
1/1/2019  31/2/2019  ce          14100      900        58

我可能更倾向于(当涉及“价格”时)使用sum,但这在很大程度上取决于您的实际意图和使用。替换min为您选择的聚合,无论是maxsum还是其他。

我会注意到,使用数字列名称有点不标准,并且可能会导致混淆(dat[,14100]将失败,dat[,\14100`] ordat[,"14100"]` 通常应该可以工作)。

您可能会发现具有数字列标题对于某些比较和描述table是有意义的,但如果您计划绘制事物(例如,使用ggplot2),通常可能会首选较长的版本(您的原始布局,尽管有总结)。


数据:

dat <- read.table(header = TRUE, stringsAsFactors = FALSE, text = "
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")

推荐阅读