比较日期时间值时提高循环启动速度

时间:2018-10-23 18:51:28

标签: r datetime for-loop

我希望加快我的一些代码,并寻求一些建议。我有两个数据框,并且想要从一个数据框中获取数据并将其输入到另一个数据框中,但是它们的格式却大不相同。我编写了一些有效的代码,但运行速度相当慢,尤其是当我拥有大型数据集时。示例数据帧如下:

dat = structure(list(Date = structure(c(1508641200, 1508643000, 1508644800, 
1508646600, 1508648400, 1508650200, 1508652000, 1508653800, 1508655600, 
1508657400, 1508659200, 1508661000, 1508662800, 1508664600, 1508666400, 
1508668200, 1508670000, 1508671800, 1508673600, 1508675400, 1508677200, 
1508679000, 1508680800, 1508682600, 1508684400, 1508686200, 1508688000, 
1508689800, 1508691600, 1508693400), class = c("POSIXct", "POSIXt"
), tzone = "EST"), X = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), tru = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("Date", "X", "tru"), row.names = c(NA, 
30L), class = "data.frame")

truth = structure(list(startdate = structure(c(1509937620, 1510705200, 
1510722240, 1512245160, 1512250560, 1512251760, 1512271140, 1512274440, 
1512984360, 1512986760, 1513002600, 1513004700, 1513752000, 1513753800, 
1513775940, 1513776840, 1514268900, 1514289000, 1514307900, 1517842620, 
1518056460, 1520031660, 1520050560, 1520075580, 1520218620), class =c("POSIXct", 
"POSIXt"), tzone = ""), enddate = structure(c(1509952320, 1510718040, 
1510737240, 1512250260, 1512269640, 1512269940, 1512290280, 1512289380, 
1513001100, 1513001400, 1513023840, 1513021440, 1513772640, 1513771440, 
1513797180, 1513796280, 1514288400, 1514307600, 1514311140, 1517857320, 
1518067560, 1520049060, 1520073240, 1520078880, 1520222820), class =c("POSIXct", 
"POSIXt"), tzone = ""), Y = c(42340.1667145732, 49765.2381579195, 
48687.3848496384, 31170.9693454847, 50435.3541955455, 49757.5112973802, 
44031.8550803252, 45912.1378875664, 47193.1529894274, 49826.4304479959, 
45840.7120690243, 42483.44259103, 52188.4048476908, 52783.8164119854, 
51769.0550080142, 49866.1301140174, 52250.5531316799, 49754.6933212176, 
45256.185763228, 47742.0544890968, 53414.0366523465, 51881.9495162963, 
48632.3656223053, 44476.3677890439, 61922.0098972212)), .Names = 
c("startdate", 
"enddate", "Y"), row.names = c(NA, 25L), class = "data.frame")

现在,我想做的是从truth$Y取值,并以它们在dat$tru和{{1 }}。

我目前拥有的是以下代码:

truth$startdate

是否有更有效的方法来避免在for循环内出现for循环?预先感谢!

1 个答案:

答案 0 :(得分:1)

使用dplyr包中的library(dplyr) for(i in 1:length(truth$startdate)){ dat$tru[between(dat$Date, truth$startdate[i], truth$enddate[i])] <-truth$Y[i] } 函数,您可以消除内部循环:

purrr

这将显着提高性能。
我相信这可以进一步完善,但这是一个快速简便的修复方法。我建议查看{{1}}软件包,它可以提供一些其他的性能改进。