遍历Date对象的结果在数字迭代器 [英] Looping over a Date object result in a numeric iterator

查看:141
本文介绍了遍历Date对象的结果在数字迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么通过日期迭代结果数字?例如:

 检测= as.Date(2009-01-01)
打印(类(测试))
#[1]日期
对于(天测试)
{
    打印(类(天))
}
#[1]数字


解决方案

?对于表示, SEQ (在后部分)为[A] n的前pression评估为向量(包括列表和一个前pression),或一个成对列表或'NULL'。

所以你的日期矢量被裹挟数字,因为日期对象没有严格载体:

  is.vector(Sys.Date())
#[1] FALSE
is.vector(as.numeric(Sys.Date()))
#[1] TRUE

Why does iterating through a Date result in numeric? For example:

test = as.Date("2009-01-01")
print( class( test ) )
# [1] "Date"
for ( day in test )
{
    print( class( day ) )
}
# [1] "numeric"

解决方案

?"for" says that seq (the part after in) is "[A]n expression evaluating to a vector (including a list and an expression) or to a pairlist or 'NULL'".

So your Date vector is being coerced to numeric because Date objects aren't strictly vectors:

is.vector(Sys.Date())
# [1] FALSE
is.vector(as.numeric(Sys.Date()))
# [1] TRUE

这篇关于遍历Date对象的结果在数字迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆