Summarize dataframe by day from timestamp

前端 未结 1 1073
终归单人心
终归单人心 2020-12-21 21:22

I have a dataset data that contains a timestamp and a suite of other variables with values at each timestamp. I am trying to use ddply within

相关标签:
1条回答
  • 2020-12-21 21:34

    You can use format to extract the day

     dat$DAY <- as.factor(format(dat$TIMESTAMP,'%d'))
    
     [1] 01 01 01 01 01 02 02 02 02 02 07 07 07 07 07
    Levels: 01 02 07
    

    For some reasons I get error if I don't remove the first column ( as said in comment in moment I am writing this)

    ddply(data[,-1],.(DAY),summarise, V1 = mean(P), V2 = max(WS))
      DAY    V1    V2
    1  01 992.4 6.259
    2  02 992.4 4.023
    3  07 239.0 3.129
    

    converting the TIMESTAMP to POSIXct seems to correct the problem

     dat$TIMESTAMP <- as.POSIXct(dat$TIMESTAMP)
    
    ddply(dat,.(DAY),summarise, V1 = mean(P), V2 = max(WS))
      DAY    V1    V2
    1  01 992.4 6.259
    2  02 992.4 4.023
    3  07 239.0 3.129
    

    EDIT no need to use format

    Since Your TIMESTAMP column is a POSIXlt , it easy to retrieve day part from it, You can do this :

    dat$TIMESTAMP$mday
     [1] 1 1 1 1 1 2 2 2 2 2 7 7 7 7 7
    
    0 讨论(0)
提交回复
热议问题