这个问题与我之前的问题类似。
我正在处理要在到期月的第一天“滚动”的财务数据。
这是我的代码,至少在mutate
部分中是错误的:
newdf2<-Data %>%
mutate(Close_exp= ifelse(as.Date(Date==Echeance)-10, lead(Close,1), Close))
newdf2= newdf2[!duplicated(newdf2$Date),]
我要执行的操作如下:创建一个名为Close_exp
的新变量,其值应为Close
,除了Echeance
的前10天。在这种情况下,它将采用以下“关闭”值。
编辑:换句话说,由于Echeance
是某些特定月份的第10天,因此它应提供Echeance
月份的第一天起的“跟随收盘价”,到Echeance月的10号。例如,假设Echeance是9月10日,我正在寻找一种方法来选择9月的前10天。
这是我的数据的一瞥:
Date Echeance Compens. Open Haut Bas Close
1 1998-03-27 00:00:00 1998-09-10 00:00:00 125. 828 828 820 820. 197
2 1998-03-27 00:00:00 1998-11-10 00:00:00 128. 847 847 842 842. 124
3 1998-03-27 00:00:00 1999-01-11 00:00:00 131. 858 858 858 858. 2
4 1998-03-30 00:00:00 1998-09-10 00:00:00 125. 821 821 820 820. 38
5 1998-03-30 00:00:00 1998-11-10 00:00:00 129. 843 843 843 843. 1
6 1998-03-30 00:00:00 1999-01-11 00:00:00 131. 860 860 860 860. 5
以下是Dput格式的数据:
structure(list(Date = structure(c(890956800, 890956800, 890956800,
891216000, 891216000, 891216000, 891302400, 891302400, 891302400,
891388800, 891388800, 891388800, 891388800, 891475200, 891475200,
891475200, 891475200, 891561600, 891561600, 891561600), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Echeance = structure(c(905385600,
910656000, 916012800, 905385600, 910656000, 916012800, 905385600,
910656000, 916012800, 905385600, 910656000, 916012800, 936921600,
905385600, 910656000, 916012800, 936921600, 905385600, 910656000,
916012800), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Compens. = c(125.00819413,
128.36207251, 130.80125679, 125.00819413, 128.51452153, 131.10615482,
125.16064315, 128.81941957, 130.95370581, 125.00819413, 128.97186858,
130.95370581, 131.10615482, 124.85574512, 129.1243176, 130.95370581,
131.10615482, 123.63615298, 128.36207251, 130.49635876), Open = c(828,
847, 858, 821, 843, 860, 820, 844, 860, 820, 846, 859, 860, 819,
846, NA, NA, 817, 845, NA), Haut = c(828, 847, 858, 821, 843,
860, 821, 845, 860, 820, 846, 859, 860, 819, 847, NA, NA, 817,
845, NA), Bas = c(820, 842, 858, 820, 843, 860, 820, 844, 860,
820, 846, 859, 860, 819, 846, NA, NA, 811, 842, NA), Close = c(819.999999969324,
841.999999974421, 858.00000000198, 819.999999969324, 842.999999992542,
859.999999972627, 820.999999987445, 845.000000028785, 859.000000020102,
819.999999969324, 845.999999981311, 859.000000020102, 859.999999972627,
819.000000016798, 846.999999999432, NA, NA, 811.000000003019,
841.999999974421, NA), Vol_Q = c(197, 124, 2, 38, 1, 5, 56, 85,
10, 10, 103, 10, 2, 30, 70, 0, 0, 145, 150, 0), Bloc_Q = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), Trades = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `Vol_€` = c(1231330.7121805,
795844.849562, 13080.125679, 237515.568847, 6425.7260765, 32776.538705,
350449.80082, 547482.5331725, 65476.852905, 62504.097065, 664205.123187,
65476.852905, 13110.615482, 187283.61768, 451935.1116, 0, 0,
896362.109105, 962715.543825, 0), O.I. = c(170, 123, 2, 188,
124, 7, 244, 184, 17, 192, 267, 27, 2, 222, 337, 27, 2, 347,
474, 27)), row.names = c(NA, -20L), class = c("tbl_df", "tbl",
"data.frame"))
非常感谢。
答案 0 :(得分:1)
我仍然不确定以下内容是否符合您的要求。
Echeance
中的某些日子等于10
,其他日子等于11
。
library(tidyverse)
library(lubridate)
Data %>%
mutate(Close_exp = ifelse(day(Echeance) %in% 10:11, Close[day(Date) %in% 1:10], Close))