igraph, POSIX, and data.table

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 06:27:29

问题


In an earlier question, I learned that graphs are useful to collapse these data

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

to obtain only those rows such that new[i] = old[i-1]. The result could then be joined into a table with users who each have their own starting points

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

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

Specifically, when only the first n=3 steps are calculated, the solution was

> 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

When implementing this when the setup is the same but new, old, and start are POSIXct class,

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

the command

> 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

returns an empty vector, which appears to be due to the inner part u[...]. I do not quite see where the problem is in this case and wonder whether anyone spots a mistake.

来源:https://stackoverflow.com/questions/57003667/igraph-posix-and-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!