【问题标题】:How to calculate monthly time differences?如何计算每月的时差?
【发布时间】:2018-11-09 13:10:40
【问题描述】:

一个简单的问题:

# Given the following initial time
t0 <- strptime('17/Nov/2010:08:04:21',format='%d/%b/%Y:%H:%M:%S')
# ...and final time
t1 <- strptime('21/Jan/2011:13:11:04',format='%d/%b/%Y:%H:%M:%S')
# I can calculate the difference of time
difftime(t1,t0,units='hours')
# Time difference of 1565.112 hours

但是,我还想知道分别对应于 11 月、12 月和 1 月的小时数。当然,我必须对来自不同年份的数千个数据执行此操作,但循环不是我的问题。任何想法?谢谢!

【问题讨论】:

  • 你能稍微弄清楚你到底需要什么输出吗?

标签: r time difference monthcalendar


【解决方案1】:

这是你的意思吗?这是一个每月计算差异的示例:

t0 <- strptime('17/Nov/2010:08:04:21',format='%d/%b/%Y:%H:%M:%S')
t1 <- strptime('21/Jan/2011:13:11:04',format='%d/%b/%Y:%H:%M:%S')

seq.firsts <- seq.Date(as.Date(t0),as.Date(t1),by='month')
boundaries <- paste0(format(seq.firsts, "%Y-%m"),"-01")

这给了你月份的界限:

> boundaries
[1] "2010-11-01" "2010-12-01" "2011-01-01"

但是现在你必须把它们整理好,如果它们进入了t0 之前,你必须把它们扔掉:

timelist <- c(t0,t1,boundaries)
ab <- sort( timelist[timelist >= t0] ) # adjust boundaries; throw out beyond t0 and t1

现在您可以在该列表中的每一对之间difftime

> for (i in 1:(length(ab)-1)) { print(abs(  difftime(ab[i],ab[i+1])   ))  }
Time difference of 81.59102 days
Time difference of 30.04167 days
Time difference of 31 days

或添加单位:

> for (i in 1:(length(ab)-1)) { print(abs(  difftime(ab[i],ab[i+1],units='hours')   ))  }
Time difference of 1958.184 hours
Time difference of 721 hours
Time difference of 744 hours

【讨论】:

  • 这正是我要找的……谢谢!
  • @mysteRious,我认为您的代码中可能存在错误。 Time difference of 31 days 不可能是正确的,因为我们正在查看从月初到 1 月 21 日的天数。
【解决方案2】:

这样的事情会起作用:

   d <- force_tz(seq(t0, t1, by="month"), tzone ="EST")

  Start<- list.append(d[[1]], lapply(d[2:length(d)], function(e) floor_date(e,"month")))

  #there is probably a cleaner way to do the next step than using  double 
  #rev() but it is get around some issues with unlist and lossing my datetime format

      End<- rev(list.append(d[[length(d)]], lapply(rev(d)[2:length(d)], function(e) ceiling_date(e,"month"))))
      df<-data.frame(Start ,End )
      df$diff<- difftime(df$End, df$Start, units ="days")
      df$diff_hours <- difftime(df$End, df$Start, units ="hours")
  df

                Start                 End          diff     diff_hours
1 2010-11-17 08:04:21 2010-12-01 00:00:00 13.66365 days 327.9275 hours
2 2010-12-01 00:00:00 2011-01-01 00:00:00 31.00000 days 744.0000 hours
3 2011-01-01 00:00:00 2011-01-17 08:04:21 16.33635 days 392.0725 hours

【讨论】:

【解决方案3】:

您可以利用as.POSIXct() 计算秒数。

# create a time vector 
sec <- seq(1, as.numeric(dif)*3.6e3, length.out=dif)

# create time table
t.dif.1 <- table(strftime(as.POSIXct(sec, origin=t0), format="%Y-%m"))
t.dif.1 <- rbind(hours=t.dif.1, "cumulated hours"=cumsum(t.dif.1)) # add cumulated hours

产量

> t.dif.1
                2010-11 2010-12 2011-01
hours               327     744     495
cumulated hours     327    1071    1566

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 2021-02-18
    • 2014-08-10
    • 2015-12-11
    • 2022-01-17
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 2021-10-28
    相关资源
    最近更新 更多