如何计算R

时间:2015-08-07 13:58:46

标签: r

我有一个数据结构,存储在一个名为created的变量中,包含时间码,我想通过计算每小时的元素来总结它们:

这是我的数据:

> class(created)
[1] "POSIXct" "POSIXt"

> head(created)
[1] "2015-07-31 13:28:17 UTC" "2015-07-31 13:28:17 UTC" "2015-07-31 13:28:10 UTC" "2015-07-31 13:28:10 UTC" "2015-07-31 13:28:10 UTC"
[6] "2015-07-31 13:28:08 UTC"

我对每小时的元素数量感兴趣,像这样:

  13:00-13:59h,1200个元素
  14:00-14:59h,2356元素
  ...

我怎样才能做到这一点?

另外我想知道,created是什么样的数据结构,我认为它必须是某种数组,List,Vector,......?

正如一些评论员要求提供原始数据一样,这里有一段简短的摘录,通过dput导出:

structure(c(1438349297, 1438349297, 1438349290, 1438349290, 1438349290), class = c("POSIXct", "POSIXt"
), tzone = "UTC")

1 个答案:

答案 0 :(得分:2)

created <- sort(sample(seq(as.POSIXct('2015-07-31 13:28:17'),len=86400*3,by='sec'),1e5,replace=T));
c(typeof(created),mode(created),class(created));
## [1] "integer" "numeric" "POSIXct" "POSIXt"
count <- table(strftime(created,'%H'));
count;
##
##   00   01   02   03   04   05   06   07   08   09   10   11   12   13   14   15   16   17   18   19   20   21   22   23
## 4236 4093 4215 4224 4245 4247 4189 4010 4201 4124 4266 4196 4221 4132 4132 4131 4264 4174 4170 4110 4102 4044 4135 4139
cat(sprintf('%1$2s:00-%1$2s:59h, %2$d Elements',names(count),count),sep='\n');
## 00:00-00:59h, 4236 Elements
## 01:00-01:59h, 4093 Elements
## 02:00-02:59h, 4215 Elements
## 03:00-03:59h, 4224 Elements
## 04:00-04:59h, 4245 Elements
## 05:00-05:59h, 4247 Elements
## 06:00-06:59h, 4189 Elements
## 07:00-07:59h, 4010 Elements
## 08:00-08:59h, 4201 Elements
## 09:00-09:59h, 4124 Elements
## 10:00-10:59h, 4266 Elements
## 11:00-11:59h, 4196 Elements
## 12:00-12:59h, 4221 Elements
## 13:00-13:59h, 4132 Elements
## 14:00-14:59h, 4132 Elements
## 15:00-15:59h, 4131 Elements
## 16:00-16:59h, 4264 Elements
## 17:00-17:59h, 4174 Elements
## 18:00-18:59h, 4170 Elements
## 19:00-19:59h, 4110 Elements
## 20:00-20:59h, 4102 Elements
## 21:00-21:59h, 4044 Elements
## 22:00-22:59h, 4135 Elements
## 23:00-23:59h, 4139 Elements
相关问题