计算R中的时间差和聚合

时间:2015-09-01 10:03:12

标签: r

我有以下数据:

        datePickup        dateAccepted
1  2015-06-30 14:30:28 2015-06-30 14:32:14
3  2015-07-03 21:25:14 2015-07-03 21:28:50
5  2015-07-03 12:27:30 2015-07-03 12:29:53

并希望汇总和平均每天的时差:

        date    averageTimeDifferenceInSeconds
1  2015-06-30   106
3  2015-07-03   179.5

我已经尝试了以下问题所示calculating time difference in R

dates <- strptime( paste(df_timestamps[,0], df_timestamps[,1]), "%Y-%m-%d %H:%M:%S")
dates <- as.numeric(difftime(strptime(paste(dates[,1],"%Y-%m-%d %H:%M:%S"),strptime(paste(dates[,2]),"%Y-%m-%d %H:%M:%S"))))

但我得到了错误:

    Error in lapply(X = x, FUN = "[", ..., drop = drop) : 
  argument is missing, with no default

3 个答案:

答案 0 :(得分:1)

所以这是一个data.table解决方案。

library(data.table)
setDT(df)[,list(Diff=mean(difftime(dateAccepted,datePickup,units="sec"))),by=as.Date(datePickup)]
#          date  Diff
# 1: 2015-06-30 106.0
# 2: 2015-07-03 179.5

打开包装:

  • setDT(df)将您的df转换为原位数据表(无需复制,因此速度非常快),
  • [,list(Diff=mean(difftime(dateAccepted,datePickup,units="sec"))),by=as.Date(datePickup)]将结果按datePickup的日期部分分组,并计算每个组的平均时差(秒)。

答案 1 :(得分:0)

....
while(1)
{
....

答案 2 :(得分:0)

vck 相同的结果。但没有&#34; xts&#34;和&#34;高频&#34;:

<?php

$c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'https://instagram.com/accounts/login/?force_classic_login');
    curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/accounts/login/?force_classic_login');
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
    curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
    $page = curl_exec($c);
    curl_close($c);

    preg_match_all('/<input type="hidden" name="csrfmiddlewaretoken" value="([A-z0-9]{32})"\/>/', $page, $token);

    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'https://instagram.com/accounts/login/?force_classic_login');
    curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/accounts/login/?force_classic_login');
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, "csrfmiddlewaretoken=".$token[1][0]."&username=[LOGIN]&password=[PASSWORD]");
    curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
    curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
    $page = curl_exec($c);
    curl_close($c);

    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, 'https://instagram.com/aaonishko/');
    curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/');
    curl_setopt($c, CURLOPT_HTTPHEADER, array(
        'Accept-Language: en-US,en;q=0.8',
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36',
        'Accept: */*',
        'X-Requested-With: XMLHttpRequest',
        'Connection: keep-alive'
        ));
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
    curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
    $page = curl_exec($c);
    curl_close($c);

    echo $page;

?>

df <- data.frame( datePickup   = strptime( c( "2015-06-30 14:30:28",
                                              "2015-07-03 21:25:14",
                                              "2015-07-03 12:27:30"  ), format="%Y-%m-%d %H:%M:%S" ),
                  dateAccepted = strptime( c( "2015-06-30 14:32:14",
                                              "2015-07-03 21:28:50",
                                              "2015-07-03 12:29:53"  ), format="%Y-%m-%d %H:%M:%S" )  )

dt <- difftime( df$dateAccepted, df$datePickup, units="secs")
date <- as.Date(df$datePickup)

avg <- data.frame( date                           = unique(date),
                   averageTimeDifferenceInSeconds = sapply( unique(date), function(d){mean(dt[which(date==d)])}) )