R中的difftime,日期/时间跨越午夜

时间:2018-03-13 15:29:12

标签: r difftime

我有一个数据集,我使用difftime来计算R中两次之间的差异。对于4天开始的记录,一天开始并持续到午夜,我得到了无意义的答案。

数据集

time2<-dput(time2)
structure(list(StationID = c(201707123L, 201710032L, 201710148L, 
201710188L), TowStartTime = structure(c(-2209057289, -2209057558, 
-2209057779, -2209057812), class = c("POSIXct", "POSIXt"), tzone = ""), 
    TowEndTime = structure(c(-2209142790, -2209143047, -2209143555, 
    -2209143587), class = c("POSIXct", "POSIXt"), tzone = ""), 
    tow_time = c(-1425.01666666667, -1424.81666666667, -1429.6, 
    -1429.58333333333)), .Names = c("StationID", "TowStartTime", 
"TowEndTime", "tow_time"), row.names = c(572L, 783L, 1003L, 1079L
), class = "data.frame") 

用于计算tow_tim的代码

time2$tow_time<-as.numeric(difftime(strptime(time2$TowEndTime,"%Y-%m-%d %H:%M:%S"),
                    strptime(time2$TowStartTime,"%Y-%m-%d %H:%M:%S")),units="mins") 

我在开始时间前有结束时间因为我不想有负值。我得到了相同的时间差的答案,无论是哪种方式都不是负值。是否有办法考虑在午夜使用difftime或R中的其他功能来执行此操作?

2 个答案:

答案 0 :(得分:0)

没有错误,在这种情况下,TowStartTime之后您有TowEndTime,因此tow_time将为负数。

示例:

StationID        TowStartTime          TowEndTime  tow_time
572  201707123 1899-12-31 05:58:31 1899-12-30 06:13:30 -1425.017

在小时内你可以看到EndTime是startTime前的23小时:

-1425.017/60
[1] -23.75028

此输出没有错,可能是原始数据。 您的负时间戳是非常见日期的convertend:

as.POSIXct(-2209143587, origin="1970-01-01")
[1] "1899-12-30 06:00:13 CET"

答案 1 :(得分:0)

您可以考虑使用lubridate包中的interval函数。

然而,你的结束时间在你的开始时间之后仍然很奇怪。

==============NVSMI LOG==============

Timestamp                           : Tue Mar 13 16:05:42 2018
Driver Version                      : 390.30

Attached GPUs                       : 1
GPU 00000000:00:04.0
    Product Name                    : Tesla K80
    Product Brand                   : Tesla
    Display Mode                    : Disabled
    Display Active                  : Disabled
    Persistence Mode                : Disabled
    Accounting Mode                 : Disabled
    Accounting Mode Buffer Size     : 1920
    Driver Model
        Current                     : N/A
        Pending                     : N/A
    Serial Number                   : 0320717075175
    GPU UUID                        : GPU-a3a146ad-aed1-d5ef-1e76-2565c1e20a13
    Minor Number                    : 0
    VBIOS Version                   : 80.21.25.00.01
    MultiGPU Board                  : No
    Board ID                        : 0x4
    GPU Part Number                 : 900-22080-6300-001
    Inforom Version
        Image Version               : 2080.0200.00.04
        OEM Object                  : 1.1
        ECC Object                  : 3.0
        Power Management Object     : N/A
    GPU Operation Mode
        Current                     : N/A
        Pending                     : N/A
    GPU Virtualization Mode
        Virtualization mode         : Pass-Through
    PCI
        Bus                         : 0x00
        Device                      : 0x04
        Domain                      : 0x0000
        Device Id                   : 0x102D10DE
        Bus Id                      : 00000000:00:04.0
        Sub System Id               : 0x106C10DE
        GPU Link Info
            PCIe Generation
                Max                 : 3
                Current             : 3
            Link Width
                Max                 : 16x
                Current             : 16x
        Bridge Chip
            Type                    : N/A
            Firmware                : N/A
        Replays since reset         : 0
        Tx Throughput               : N/A
        Rx Throughput               : N/A
    Fan Speed                       : N/A
    Performance State               : P0
    Clocks Throttle Reasons
        Idle                        : Not Active
        Applications Clocks Setting : Active
        SW Power Cap                : Not Active
        HW Slowdown                 : Not Active
            HW Thermal Slowdown     : N/A
            HW Power Brake Slowdown : N/A
        Sync Boost                  : Not Active
        SW Thermal Slowdown         : Not Active
        Display Clock Setting       : Not Active
    FB Memory Usage
        Total                       : 11441 MiB
        Used                        : 10930 MiB
        Free                        : 511 MiB
    BAR1 Memory Usage
        Total                       : 16384 MiB
        Used                        : 3 MiB
        Free                        : 16381 MiB
    Compute Mode                    : Default
    Utilization
        Gpu                         : 54 %
        Memory                      : 1 %
        Encoder                     : 0 %
        Decoder                     : 0 %
    Encoder Stats
        Active Sessions             : 0
        Average FPS                 : 0
        Average Latency             : 0
    Ecc Mode
        Current                     : Enabled
        Pending                     : Enabled
    ECC Errors
        Volatile
            Single Bit            
                Device Memory       : 0
                Register File       : 0
                L1 Cache            : 0
                L2 Cache            : 0
                Texture Memory      : 0
                Texture Shared      : N/A
                CBU                 : N/A
                Total               : 0
            Double Bit            
                Device Memory       : 0
                Register File       : 0
                L1 Cache            : 0
                L2 Cache            : 0
                Texture Memory      : 0
                Texture Shared      : N/A
                CBU                 : N/A
                Total               : 0
        Aggregate
            Single Bit            
                Device Memory       : 0
                Register File       : 0
                L1 Cache            : 0
                L2 Cache            : 0
                Texture Memory      : 0
                Texture Shared      : N/A
                CBU                 : N/A
                Total               : 0
            Double Bit            
                Device Memory       : 7
                Register File       : 0
                L1 Cache            : 0
                L2 Cache            : 0
                Texture Memory      : 0
                Texture Shared      : N/A
                CBU                 : N/A
                Total               : 7
    Retired Pages
        Single Bit ECC              : 0
        Double Bit ECC              : 1
        Pending                     : No
    Temperature
        GPU Current Temp            : 56 C
        GPU Shutdown Temp           : 93 C
        GPU Slowdown Temp           : 88 C
        GPU Max Operating Temp      : N/A
        Memory Current Temp         : N/A
        Memory Max Operating Temp   : N/A
    Power Readings
        Power Management            : Supported
        Power Draw                  : 66.18 W
        Power Limit                 : 149.00 W
        Default Power Limit         : 149.00 W
        Enforced Power Limit        : 149.00 W
        Min Power Limit             : 100.00 W
        Max Power Limit             : 175.00 W
    Clocks
        Graphics                    : 562 MHz
        SM                          : 562 MHz
        Memory                      : 2505 MHz
        Video                       : 540 MHz
    Applications Clocks
        Graphics                    : 562 MHz
        Memory                      : 2505 MHz
    Default Applications Clocks
        Graphics                    : 562 MHz
        Memory                      : 2505 MHz
    Max Clocks
        Graphics                    : 875 MHz
        SM                          : 875 MHz
        Memory                      : 2505 MHz
        Video                       : 540 MHz
    Max Customer Boost Clocks
        Graphics                    : N/A
    Clock Policy
        Auto Boost                  : On
        Auto Boost Default          : On