R中的可扩展结转以创建每日时间序列

时间:2013-03-30 02:07:12

标签: r bigdata zoo data-manipulation

我正在尝试从当前仅定期观察的内容创建每日时间序列数据集。我可以成功地为单个案例执行所需的操作,但无法确定如何缩放到整个数据集。例如:

        UNIT <- c(100,100, 200, 200, 200, 200, 200, 300, 300, 300,300)
        STATUS <- c('ACTIVE','INACTIVE','ACTIVE','ACTIVE','INACTIVE','ACTIVE','INACTIVE','ACTIVE','ACTIVE',
                    'ACTIVE','INACTIVE') 
        TERMINATED <- as.Date(c('1999-07-06' , '2008-12-05' , '2000-08-18' , '2000-08-18' ,'2000-08-18' ,'2008-08-18',
                        '2008-08-18','2006-09-19','2006-09-19' ,'2006-09-19' ,'1999-03-15')) 
        START <- as.Date(c('2007-04-23','2008-12-06','2004-06-01','2007-02-01','2008-04-19','2010-11-29','2010-12-30',
                   '2007-10-29','2008-02-05','2008-06-30','2009-02-07'))
        STOP <- as.Date(c('2008-12-05','2012-12-31','2007-01-31','2008-04-18','2010-11-28','2010-12-29','2012-12-31',
                  '2008-02-04','2008-06-29','2009-02-06','2012-12-31'))
        TEST <- data.frame(UNIT,STATUS,TERMINATED,START,STOP)
        TEST                   

对间隔内单位的观察:

   UNIT   STATUS TERMINATED      START       STOP
1   100   ACTIVE 1999-07-06 2007-04-23 2008-12-05
2   100 INACTIVE 2008-12-05 2008-12-06 2012-12-31
3   200   ACTIVE 2000-08-18 2004-06-01 2007-01-31
4   200   ACTIVE 2000-08-18 2007-02-01 2008-04-18
5   200 INACTIVE 2000-08-18 2008-04-19 2010-11-28
6   200   ACTIVE 2008-08-18 2010-11-29 2010-12-29
7   200 INACTIVE 2008-08-18 2010-12-30 2012-12-31
8   300   ACTIVE 2006-09-19 2007-10-29 2008-02-04
9   300   ACTIVE 2006-09-19 2008-02-05 2008-06-29
10  300   ACTIVE 2006-09-19 2008-06-30 2009-02-06
11  300 INACTIVE 1999-03-15 2009-02-07 2012-12-31            

我想在START的整个范围内拍摄每个单位并复制“STATUS”和“TERMINATE”(以及大数据集中的其他N个协变量)每日的值和结束日期。为单个记录做这个......

        A <-  seq(TEST$START[1], TEST$STOP[1], "days") #vector of relevant date sequences 

        #keeping the old data, now with daily date "fill" 
        B <- matrix(NA, length(A), dim(TEST[-c(4,5)])[2]) 
        C <- data.frame(A,B)

        #carry forward observations on covariates through date range 
        TEST[-c(4,5)][1,]  #note terminated has the proper date status:
        UNIT STATUS TERMINATED
         1  100 ACTIVE 1999-07-06

        #now the TERMINATED loses its 'date' status for some reason
        C[-c(1)][1,] <- TEST[-c(4,5)][1,] 
        D <-  na.locf(C)
        colnames(D)[2:4] <-colnames(TEST)[1:3]
        colnames(D)[1] <- "DATE"
        head(D)

        DATE UNIT STATUS TERMINATED
1 2007-04-23  100      1      10778
2 2007-04-24  100      1      10778
3 2007-04-25  100      1      10778
4 2007-04-26  100      1      10778
5 2007-04-27  100      1      10778
6 2007-04-28  100      1      10778

第一行的观察结果在START到END的范围内重复,并创建一个新的向量:整个时期的每日时间序列。我想对第2行执行此操作,将其绑定到D,然后由UNIT分析。我用na.locf写了一个for循环,试图概括一下:

for(i in 1:nrow(TEST)){
  for(j in 0:nrow(TEST)-1) {
  A <-  seq(TEST$START[i], TEST$STOP[i], "days")

  B <- matrix(NA, length(A), dim(TEST[-c(4,5)])[2])
  C <- data.frame(A,B)

  C[-c(1)][1,] <- TEST[-c(4,5)][i,] 
  assign(paste("D",i, sep=""),na.locf(C)) 

  #below here the code does not work. R does not recognize i and j as I intend
  #I haven't been able to overcome this using assign, evaluate etc. 
  colnames(Di)[2:4] <-colnames(TEST)[1:3]
  colnames(Di)[1] <- "DATE"

  D0 <- matrix(NA, 1, dim(Di)[2])
  assign(paste("D", j, sep = ""),Dj)
  rbind(Di,Dj)

   }
  }            

单一记录“解决方案”的明显问题是处理“终止”日期。就在使用na.locf之前,它会丢失它的Date状态。

我希望有一个更好的方式来看待这个,我只是因为无知而把自己埋没在并发症中。

1 个答案:

答案 0 :(得分:2)

在SQL中相对容易,因此可以使用sqldf, 它将data.frames视为SQL表。

dates <- data.frame( date = seq.Date( min(TEST$START), max(TEST$STOP), by = 1 ) )
library(sqldf)
result <- sqldf( "
  SELECT *
  FROM TEST, dates
  WHERE START <= date AND date <= STOP
" )
head( result )

如果数据很大,可能值得将数据存储在数据库中, 并在那里进行计算。

# With SQLite, a database is just a file
library(RSQLite)
connection <- dbConnect( SQLite(), "/tmp/test.db" )  

# Copy the data.frames to the "Test" and "Dates" table.
# When transfering data across systems, it is often easier 
# to convert dates to strings.
convert_dates <- function(d) {
  as.data.frame( lapply( 
    d, 
    function(u) if( "Date" %in% class(u) ) as.character(u) else u 
  ) ) 
}
dbWriteTable(connection, "Test",  convert_dates(TEST),  row.names = FALSE )
dbWriteTable(connection, "Dates", convert_dates(dates), row.names = FALSE )

# Check how many rows the query has: it could be 
# that the result does not fit in memory
dbGetQuery( connection, "
  SELECT COUNT(*) 
  FROM   Test, Dates 
  WHERE  start <= date AND date <= stop
" )

# If it is reasonable, retrieve all the data
dbGetQuery( connection, "
  SELECT * 
  FROM   Test, Dates 
  WHERE  start <= date AND date <= stop
" )

# If not, only retrieve what you need
dbGetQuery( connection, "
  SELECT * 
  FROM   Test, Dates 
  WHERE  start <= date AND date <= stop
  AND    '2013-04-01' <= date AND date <= '2013-04-30'
" )