I liked so much index search of xts
that I created one for data.frame
.
"%d%"=function(df1,dt,col='dt_pregao') {
if(regexpr('/',dt)>0){
dt=strsplit(dt,'/')[[1]]
return(df1[df1[,col] %>=<% c(dt[1],dt[2]),])}
return(df1[df1[,col]==dt,])}
This works:
opx%d%'2018-03-09'
I saw that I can put the column name as a parameter.
But how can I pass the third parameter using infix notation? Neither of these works:
opx%d%c('2018-03-09','dt_pregao')
(opx%d%'2018-03-09')'dt_pregao'
P.S.
"%>=<%"<-function(x,rng) x>=rng[1] & x<=rng[2]
opx
example:
structure(list(dt_pregao = structure(c(17487, 17487, 17487, 17487,
17599, 17599, 17599, 17599, 17599, 17599), class = "Date"), cd_papel = c("PETRK16",
"PETRK81", "PETRK18", "PETRK4", "PETRK47", "PETRK48", "PETRK53",
"PETRK7", "PETRK70", "PETRK1"), veFech = c(0.08, 0, 0.01, 0.01,
0.01, 0.01, 0.03, -9.06, 0.12, -0.01)), .Names = c("dt_pregao",
"cd_papel", "veFech"), row.names = c(NA, -10L), class = "data.frame")
To call an infix function with more than 2 arguments you will need to use its prefix form:
`%sum%` <- function(x, y, z = 0) x + y + z
4 %sum% 5
## [1] 9
`%sum%`(4, 5, 6)
## [1] 15
Alternately redefine it so that it only requires 2 arguments.