r - 如何为我的问题使用应用系列而不是嵌套的 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
解决方案
这是一个 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
为您选择的聚合,无论是max
、sum
还是其他。
我会注意到,使用数字列名称有点不标准,并且可能会导致混淆(dat[,14100]
将失败,dat[,\
14100`] or
dat[,"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")
推荐阅读
- angular - 如何在函数 gleich 中使用变量 rechenoperator2?
- javascript - 如果数组包含重复项,则将两个值合并为一个
- entity-framework-core - 使用 GROUP BY 检索组中的最新行
- google-bigquery - 来自以太坊的 BigQuery 数据中可用的指标
- javascript - 错误时的javascript axios控制台日志响应
- java - 没有可用的“com.milkdairy.dao.userRepository”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean
- macros - 从 Enter 确认 Atom 解除绑定更正
- python - 为什么 Python XLSWRITER 单元格公式不能以编程方式工作?
- linux-kernel - 将 writecombine 区域映射到用户空间文档
- javascript - 输入框通过单击按钮来提醒文本(对 JQuery 来说真的很新)