R-在一系列行上使用mutate创建新变量

R-在一系列行上使用mutate创建新变量,r,R,这个问题与我先前的问题相似 我正在处理我想在到期月份的第一天滚动的财务数据。 这是我的代码,至少在变异部分是错误的: 以下是Dput格式的数据: structure(list(Date = structure(c(890956800, 890956800, 890956800, 891216000, 891216000, 891216000, 891302400, 891302400, 891302400, 891388800, 891388800, 891388800, 891388

这个问题与我先前的问题相似

我正在处理我想在到期月份的第一天滚动的财务数据。 这是我的代码,至少在变异部分是错误的:

以下是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"))

事先非常感谢。

我仍然不确定以下内容是否符合您的要求。 有些天在e意义上等于10,有些天等于11


由于.DateDate==Echeance是错误的来源,因此比较==返回一个逻辑值,您正试图将其强制为FALSE/TRUE到类Date@我明白了,谢谢你的回复。在这种情况下,我如何在前10天设置条件?您能否以dput格式发布示例数据?请使用dputnewdf的输出编辑问题。或者,如果dputheadnewdf的输出太大,则为20。我将数据放在dput格式中。它是数据,而不是newdf,因为它是我正在处理的。所以Close_exp等于Close,除非Date==Echeance-10,它取什么值?下一个收盘价是多少?这就是leadClose,1在本例中的含义。不完全是这样,我将其更改为:如果monthDate==monthEcheance{newdf3%mutateClose_exp=ifelseDate%in%1:10,leadClose,1,Close}这转换了我想要做的事情,但我认为我的代码是错误的,你能检查一下吗?我试过了,它似乎在%1:10,leadClose,1,Close中新起作用了
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  
 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"))
library(tidyverse)
library(lubridate)

Data %>%
  mutate(Close_exp = ifelse(day(Echeance) %in% 10:11, Close[day(Date) %in% 1:10], Close))