提取动物园对象中唯一天数的更好方法

时间:2013-07-09 00:47:10

标签: r zoo

我的数据如下所示。

> head(data)
                  open   high    low  close
2013-06-20 09:31:00 275.50 276.00 275.08 275.65
2013-06-20 09:32:00 275.61 276.88 275.61 276.67
2013-06-20 09:33:00 276.67 276.72 275.95 276.62
2013-06-20 09:34:00 276.48 277.43 276.27 277.00
2013-06-20 09:35:00 277.00 278.00 277.00 278.00
2013-06-20 09:36:00 277.83 277.97 276.58 277.29

我输入以下内容获得了独特日期的数量:

length(levels(as.factor(floor(as.numeric(julian(index(data)))))))

有更好的方法吗?我希望有一个numdays.zoo()函数或其他东西。

2 个答案:

答案 0 :(得分:2)

我会使用xts::ndays

library(xts)
ndays(data)

但是如果你不想加载另一个包,你可以这样做

length(unique(as.Date(time(data))))

答案 1 :(得分:0)

您可以将索引转换为日期格式并使用table

table(format(index(data),'%y-%m-%d'))
13-06-20 
       6

然后天数就是:

  length(names(table(format(index(data),'%y-%m-%d'))))