Using group_by with difftime

后端 未结 2 1200
夕颜
夕颜 2021-01-26 18:44

I\'ve created a dataframe with data :

idCol <- c(\'1\',\'1\',\'2\',\'2\')
stepCol <- c(\'step1\' , \'step2\' , \'step1\' , \'step2\')
timestampCol <- c         


        
相关标签:
2条回答
  • 2021-01-26 19:17

    I did some minor editing to your code but basically you need to associate the results of ymd_hms with your mydata:

    mydata$diffTime <- c(0, difftime(lubridate::ymd_hms(mydata$timestamp[-1]), 
                              lubridate::ymd_hms(mydata$timestamp[-nrow(mydata)]), units="hours"))
    diffTime <- mydata %>% group_by(id) %>% summarize(mean(diffTime))
    

    Returns:

    R> diffTime
    # A tibble: 2 x 2
         id `mean(diffTime)`
      <chr>            <dbl>
    1     1         0.008333
    2     2         0.033333
    
    0 讨论(0)
  • 2021-01-26 19:21

    Please note there is inconsistencies in the sample data timestamp column for the time nomenclature

    timestampCol <- c('01-01-2017:09.00', '01-01-2017:10.00', '01-01-2017:09.00', '01-01-2017:14.00')
    

    Converting strings to time values (accounting for factors)

    mydata$timestamp <- as.POSIXct(strptime(levels(mydata$timestamp)[mydata$timestamp], format="%m-%d-%Y:%H.%M"))
    
    library(dplyr)
    mydata %>%
      group_by(id) %>%
      mutate(diff = difftime(timestamp, lag(timestamp))) %>%
      summarise(na.omit(diff))
    
    # A tibble: 2 x 2
          id `na.omit(diff)`
      <fctr>          <time>
    1      1         1 hours
    2      2         5 hours
    
    0 讨论(0)
提交回复
热议问题