zoukankan      html  css  js  c++  java
  • R 时间戳转化

    转 http://stackoverflow.com/questions/1962278/dealing-with-timestamps-in-r

    You want the (standard) POSIXt type from base R that can be had in 'compact form' as a POSIXct (which is essentially a double representing fractional seconds since the epoch) or as long form in POSIXlt (which contains sub-elements). The cool thing is that arithmetic etc are defined on this -- see help(DateTimeClasses)

    Quick example:

    R> now <- Sys.time()
    R> now
    [1] "2009-12-25 18:39:11 CST"
    R> as.numeric(now)
    [1] 1.262e+09
    R> now + 10  # adds 10 seconds
    [1] "2009-12-25 18:39:21 CST"
    R> as.POSIXlt(now)
    [1] "2009-12-25 18:39:11 CST"
    R> str(as.POSIXlt(now))
     POSIXlt[1:9], format: "2009-12-25 18:39:11"
    R> unclass(as.POSIXlt(now))
    $sec
    [1] 11.79
    
    $min
    [1] 39
    
    $hour
    [1] 18
    
    $mday
    [1] 25
    
    $mon
    [1] 11
    
    $year
    [1] 109
    
    $wday
    [1] 5
    
    $yday
    [1] 358
    
    $isdst
    [1] 0
    
    attr(,"tzone")
    [1] "America/Chicago" "CST"             "CDT"            

    As for reading them in, see help(strptime)

    As for difference, easy too:

    R> Jan1 <- strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S")
    R> difftime(now, Jan1, unit="week")
    Time difference of 51.25 weeks

    Lastly, the zoo package is an extremely versatile and well-documented container for matrix with associated date/time indices.

    timestamp to datestr:

    datestr = as.character(as.POSIXlt(timestamp, origin="1970-01-01"))
  • 相关阅读:
    JAVA基础学习(7)之函数
    人与神话阅读笔记03
    学习进度八
    人月神话阅读笔记02
    NABCD原则
    人月神话阅读笔记01
    学习进度七
    学习进度六
    梦断代码阅读笔记03
    地铁系统初步思路
  • 原文地址:https://www.cnblogs.com/shalijiang/p/4523863.html
Copyright © 2011-2022 走看看