且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在2个日期之间对数据框进行分组

更新时间:2023-11-18 23:05:28

正如@MrFlick所指出的那样,您不会绕过子集的基本逻辑。一种简化子集特定数据框架的方法是定义一个函数,该函数接受两个输入,例如 DATE1 DATE2 在您的示例中,然后根据那些子集参数返回 IBOV_RET 的子集。

As already pointed out by @MrFlick, you dont get around the basic logic of subsetting. One way to make it easier for you to subset your specific data.frame would be to define a function that takes two inputs like DATE1 and DATE2 in your example and then returns the subset of IBOV_RET according to those subset parameters.

myfunc <- function(x,y){IBOV_RET[IBOV_RET$DATE >= x & IBOV_RET$DATE <= y,]}

DATE1 <- as.Date("1993-04-29")
DATE2 <- as.Date("1993-05-04")

Test <- myfunc(DATE1,DATE2)    

#> Test
#        DATE  X1D_RETURN
#2 1993-04-29 -0.02469136
#3 1993-04-30  0.01687764
#4 1993-05-03  0.00000000
#5 1993-05-04  0.03319502

您也可以直接在 myfunc

myfunc(as.Date("1993-04-29"),as.Date("1993-05-04")) #will produce the same result