Sorting data frame based on month-year time format

后端 未结 6 2142
孤城傲影
孤城傲影 2021-01-03 06:29

I\'m struggling with something very basic: sorting a data frame based on a time format (month-year, or, “%B-%y” in this case). My goal is to calculate various monthly statis

6条回答
  •  暖寄归人
    2021-01-03 06:45

    Edit: I misunderstood the question at first. Copy the data given in the question first, then

    > tmp09 <- read.table(file="clipboard", header=TRUE)
    > Sys.setlocale(category="LC_TIME", locale="Dutch_Belgium.1252")
    [1] "Dutch_Belgium.1252"
    
    # create POSIXlt variable from monthYear
    > tmp09$d <- strptime(paste("2007", tmp09$monthYear, sep="-"), "%Y-%B-%d")
    
    # create ordered factor
    > tmp09$dFac <- droplevels(cut(tmp09$d, breaks="month", ordered=TRUE))
    > tmp09[order(tmp09$d), ]
       Instrument AccountValue   monthYear   ExitTime          d       dFac
    1         JPM         6997    april-07 2007-04-10 2007-04-07 2007-04-01
    2         JPM         7261      mei-07 2007-05-29 2007-05-07 2007-05-01
    11        KFT         6992      mei-07 2007-05-14 2007-05-07 2007-05-01
    12        KFT         6944      mei-07 2007-05-21 2007-05-07 2007-05-01
    3         JPM         7545     juli-07 2007-07-18 2007-07-07 2007-07-01
    4         JPM         7614     juli-07 2007-07-19 2007-07-07 2007-07-01
    13        KFT         7069     juli-07 2007-07-09 2007-07-07 2007-07-01
    14        KFT         6919     juli-07 2007-07-16 2007-07-07 2007-07-01
    5         JPM         7897 augustus-07 2007-08-22 2007-08-07 2007-08-01
    10        JPM         7423 november-07 2007-11-02 2007-11-07 2007-11-01
    
    > Tmp09Totals <- tapply(tmp09$AccountValue, tmp09$dFac, sum)
    > Tmp09Totals
    2007-04-01 2007-05-01 2007-07-01 2007-08-01 2007-11-01 
          6997      21197      29147       7897       7423
    

提交回复
热议问题