r由id变量提供

时间:2015-09-11 15:16:46

标签: r lapply

我有两个数据框,DFA包含日期和ID,其他DFB包含id,以及两列日期date0,date1。 我想要一个函数在DFB中搜索DFA中的日期,如果日期在de范围内则创建一个列表,如果不是,则创建0。 经过几次试验,我有这个功能:

completa_roles <- function(x, y, z) {
for(i in 1: length(y)){ 
    if ((x >= y[i] & x <= z[i])==TRUE) {
      m[i] <- 1
      } else {
      m[i] <- 0 
      }
    }
 return(m)
}

lapply(x, FUN=completa_roles, y=DFB$date0, z=DFB$date1)

这项工作可以用于一个id,但我不能通过id变量拆分函数,我试过,tapply和dlply。帮助将是apreciated。 例如:

DFA <- data.frame(id= c(3,3,3,3,3,3,5,5,5), x=c("2007-07-01", "2007-08-01", "2007-09-01",
                                          "2007-10-01", "2007-11-01", "2007-12-01", 
                                          "2010-06-01", "2009-06-01", "2008-06-01"),
              stringsAsFactors = FALSE)
DFB <- data.frame(id=c(3,3,3,3,5,5), date0=c("2010-01-01", "2008-02-20", "2007-04-02",
               "2012-09-01", "2000-06-01", "2004-06-01"), date1=c("2015-06-30", "2009-12-31", "2012-08-31",
               "2015-06-30", "2002-06-01", "2008-06-01"),
              stringsAsFactors = FALSE)
DFA$x <- as.Date(DFA$x, origin = "1970-01-01")
DFB$date0 <- as.Date(DFB$date0, origin = "1970-01-01")
DFB$date1 <- as.Date(DFB$date1, origin = "1970-01-01")

m <- list()
completa_roles <- function(x, y, z) {
for(i in 1: length(y)){ 
if ((x >= y[i] & x <= z[i])==TRUE) {
  m[i] <- 1
} else {
  m[i] <- 0 
 }
 }
return(m)
}

x <- DFA$x
res <- lapply(x, FUN=completa_roles, y= DFB$date0, z= DFB$date1)

1 个答案:

答案 0 :(得分:1)

如果我正确理解了你想要的东西,那么在语法上更清晰的方法就是start_date=serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S') dplyr库。您只想进行连接并检查x是否在区间内:

lubridate