lubridateパッケージ入門

27
lubridateパッケージで 君も付処スタ☆ Tokyo.R#50 20150905 @kashitan lubridate パッケージ入門

Transcript of lubridateパッケージ入門

  • lubridate

    !

    Tokyo.R#50 2015-09-05 @kashitan

    lubridate

  • #Tsurami

    lubridate

  • Date

    !

    POSIXct(POSIX calendar time) POSIXlt(POSIX local time)

  • Date 197011

    >#1970110>as.Date("1970-01-01")%>%+as.integer()[1]0>>#2015953>as.Date("2015-09-05")+3[1]"2015-09-08">>#(difftime)>(as.Date("2015-09-05")-as.Date("2015-09-01"))%>%+as.integer()[1]4

  • POSIXct 197011000

    >#1970110000>as.POSIXct("1970-01-0100:00:00",tz="UTC")%>%+as.integer()[1]0>>#2015953>as.POSIXct("2015-09-0512:34:56",tz="Japan")+3*60*60[1]"2015-09-0515:34:56JST"

  • POSIXlt

    >todayunclass(today)$sec[1]56$min[1]34$hour[1]12...>>#>today$hour[1]12

  • #Tsurami

  • #Tsurami

    format

    POSIXlt

  • format yyyy-mm-dd, yyyy/mm/ddformat

    >as.Date("20150905")charToDate(x):> >as.Date("20150905",format="%Y%m%d")[1]"2015-09-05"> >as.POSIXlt("20150905123456",format="%Y%m%d%H%M%S")[1]"2015-09-0512:34:56JST"

    %m%M

  • POSIXlt $year, $mon

    >today#$year1900>today$year[1]115>>#$mon0-11>today$mon[1]8>>as.POSIXlt("2016-01-01")$mon[1]0

    1900

    1

  • format(ry

    >#30>dd+months(1)[1]"2015-02-01""2015-03-02">d+days(30)[1]"2015-01-31""2015-03-04">>#>dd[1]"2015-09-01""2015-09-02"

  • Whats lubridate? Date, POSIXt

    Hadley

  • Whats lubridate? Date

    P.32 Hadley Wickham, an author of several popular R packages, has often made use of this data set in illustrating data manipulation in R.

  • #Tsurami

    format

    POSIXlt

  • format ymd(), mdy(),dmy()format

    >library(lubridate)!>ymd("2015-09-05")[1]"2015-09-05UTC">ymd("20150905")[1]"2015-09-05UTC"!>mdy("Sep-05-2015",locale="C")[1]"2015-09-05UTC">>dmy("05/09/15")[1]"2015-09-05UTC"

  • format ymd(), mdy(),dmy()format

    >library(lubridate)!>ymd("2015-09-05")[1]"2015-09-05UTC">ymd("20150905")[1]"2015-09-05UTC"!>mdy("Sep-05-2015",locale="C")[1]"2015-09-05UTC">>dmy("05/09/15")[1]"2015-09-05UTC"

    locale=C

  • format ymd_hms(), mdy_hms()POSIXt

    >ymd_hms("20150905123456")[1]"2015-09-0512:34:56UTC"!>options(digits.secs=3)>ymd_hms("2015-09-0512:34:56.789")[1]"2015-09-0512:34:56.789UTC"!>mdy_hms("Sep-05-201512:34:56.789",locale="C")[1]"2015-09-0512:34:56.789UTC"

  • format

    >#creatingheterogeneousdateobject>hetero_date#parsingthecharacterdateobjectandconverttovaliddate>ymd(hetero_date,locale="C")[1]"2013-08-24UTC""2013-08-18UTC""2013-08-23UTC"

  • POSIXlt year(),month()

    >#>ctct[1]"2015-09-0501:20:26.254JST"!>#>year(ct)[1]2015!>#(label=TRUE)>month(ct)[1]9

  • POSIXlt year(),month()

    >#(day or mday)>day(ct)[1]5>#>wday(ct)[1]7>#>week(ct)[1]36>#>yday(ct)[1]248

  • POSIXlt year(),month()

    >#>hour(ct)[1]1>#>minute(ct)[1]20>#>second(ct)[1]26.25421

  • year(),month()

    >dd[1]"2015-01-01UTC""2015-02-02UTC!>#9>month(d)d[1]"2015-09-01UTC""2015-09-02UTC!>#2016>year(d)d[1]"2016-09-01UTC""2016-09-02UTC"

  • ? with_tz()

    !

    leap_year()

    >#>with_tz(now(),"America/New_York")[1]"2015-09-0413:04:21.272EDT

    >#>dd[1]"2015-01-01UTC""2016-01-01UTC""2017-01-01UTC"!>leap_year(d)[1]FALSETRUEFALSE

  • lubridate!!