首页 > 解决方案 > igraph、POSIX 和 data.table

问题描述

较早的问题中,我了解到图表对于折叠这些数据很有用。

require(data.table)    
set.seed(333)
t <- data.table(old=1002:2001, dif=sample(1:10,1000, replace=TRUE))
t$new <- t$old + t$dif; t$foo <- rnorm(1000); t$dif <- NULL

> head(t)
    old  new        foo
1: 1002 1007 -0.7889534
2: 1003 1004  0.3901869
3: 1004 1014  0.7907947
4: 1005 1011  2.0964612
5: 1006 1007  1.1834171
6: 1007 1015  1.1397910

只获得那些行,使得new[i] = old[i-1]. 然后可以将结果连接到一个表中,每个用户都有自己的起点

i <- data.table(id=1:3, start=sample(1000:1990,3))

    > i
   id start
1:  1  1002
2:  2  1744
3:  3  1656

具体来说,当只n=3计算第一步时,解决方案是

> library(igraph)
> i[, t[old %in% subcomponent(g, start, "out")[1:n]], by=.(id)]
   id  old  new        foo
1:  1 1002 1007 -0.7889534
2:  1 1007 1015  1.1397910
3:  1 1015 1022 -1.2193666
4:  2 1744 1750 -0.1368320
5:  2 1750 1758  0.3331686
6:  2 1758 1763  1.3040357
7:  3 1656 1659 -0.1556208
8:  3 1659 1663  0.1663042
9:  3 1663 1669  0.3781835

当设置相同但new, old, 和start是 POSIXct 类时执行此操作,

set.seed(333)
u <- data.table(old=seq(from=as.POSIXct("2013-01-01"), 
                          to=as.POSIXct("2013-01-02"), by="15 mins"), 
                dif=as.difftime(sample(seq(15,120,15),97,replace=TRUE),units="mins"))
u$new <- u$old + u$dif; u$foo <- rnorm(97); u$dif <- NULL
j <- data.table(id=1:3, start=sample(seq(from=as.POSIXct("2013-01-01"), 
                                       to=as.POSIXct("2013-01-01 22:00:00"), by="15 mins"),3))

> head(u)
                   old                 new        foo
1: 2013-01-01 00:00:00 2013-01-01 01:00:00 -1.5434407
2: 2013-01-01 00:15:00 2013-01-01 00:30:00 -0.2753971
3: 2013-01-01 00:30:00 2013-01-01 02:30:00 -1.5986916
4: 2013-01-01 00:45:00 2013-01-01 02:00:00 -0.6288528
5: 2013-01-01 01:00:00 2013-01-01 01:15:00 -0.8967041
6: 2013-01-01 01:15:00 2013-01-01 02:45:00 -1.2145590

> j
   id               start
1:  1 2013-01-01 22:00:00
2:  2 2013-01-01 21:00:00
3:  3 2013-01-01 13:30:00

命令

> j[, u[old %in% subcomponent(h, V(h)$name %in% as.character(start), "out")[1:n]], by=.(id)]
Empty data.table (0 rows and 4 cols): id,old,new,foo

返回一个空向量,这似乎是由于内部部分u[...]。在这种情况下,我不太明白问题出在哪里,想知道是否有人发现了错误。

标签: rdata.tableposixigraph

解决方案


推荐阅读