在动物园工作几个月

发布于 2024-12-03 04:45:56 字数 1566 浏览 0 评论 0原文

我想用一个变量来增强动物园对象,我可以用它来测试月份的变化。我确信有更通用的方法可以做到这一点。那里的建议会很好,但我想了解为什么这种简单的方法会失败。如果我明白我在这里缺少什么,我会感觉更好;-)

例如,对于动物园对象,

library(zoo)
tz <- structure(c(7L, 7L, 1L, 6L, 0L, 9L, 0L, 1L, 6L, 0L, 3L, 3L, 5L, 
0L, 8L, 2L, 0L, 3L, 2L, 5L, 2L, 3L, 4L, 7L, 8L, 9L, 0L, 1L, 4L, 
5L, 6L, 7L, 8L, 2L, 3L, 4L, 5L, 8L, 9L, 0L), .Dim = c(20L, 2L
), .Dimnames = list(NULL, c("x", "y")), index = structure(c(13880, 
13881, 13913, 13916, 13946, 13947, 13948, 13980, 13983, 13984, 
13985, 14016, 14048, 14082, 14083, 14115, 14147, 14180, 14212, 
14243), class = "Date"), class = "zoo")

使用 as.yearmon() 添加年/月变量似乎很容易。如果我在数据框中,这将产生一个很好的字符变量,但在动物园中,如果你忘记换行 as.numeric() ,就会发生悲剧,

tz$yrmo <- as.numeric(as.yearmon(index(tstz)))
> head(tz)
           x y     yrmo
2008-01-02 7 2 2008.000
2008-01-03 7 3 2008.000
2008-02-04 1 4 2008.083
2008-02-07 6 7 2008.083
2008-03-08 0 8 2008.167
2008-03-09 9 9 2008.167

这看起来很棒,我可以成功比较数据元素

(tz$x[6] != tz$y[6])
2008-03-09
错误

但为什么当我比较年/月变量时会得到这个结果?

> (tz$yrmo[2] != tz$yrmo[1])
Data:
logical(0)

Index:
character(0)

为什么使用相同()测试yearmon或数据项会以这种方式失败? (两者都应该是真的)

> identical(tz$yrmo[2] , tz$yrmo[1])
[1] FALSE
> identical(tz$x[2] , tz$x[1])
[1] FALSE

我在使用yearmon()在动物园中创建索引类时只是在玩火吗? 我应该改用 Dirk Eddelbuettel 的“将日期转换为月份数字”吗? 两个日期之间的月数

I'd like to augment a zoo object with a variable which I could use to test for month changes. I'm sure there are more general ways to do this. Suggestions there would be great, but I'd like to understand why this simple approach fails. I'd feel better if I understood what I'm missing here ;-)

e.g. for a zoo object

library(zoo)
tz <- structure(c(7L, 7L, 1L, 6L, 0L, 9L, 0L, 1L, 6L, 0L, 3L, 3L, 5L, 
0L, 8L, 2L, 0L, 3L, 2L, 5L, 2L, 3L, 4L, 7L, 8L, 9L, 0L, 1L, 4L, 
5L, 6L, 7L, 8L, 2L, 3L, 4L, 5L, 8L, 9L, 0L), .Dim = c(20L, 2L
), .Dimnames = list(NULL, c("x", "y")), index = structure(c(13880, 
13881, 13913, 13916, 13946, 13947, 13948, 13980, 13983, 13984, 
13985, 14016, 14048, 14082, 14083, 14115, 14147, 14180, 14212, 
14243), class = "Date"), class = "zoo")

Add a year/month variable using as.yearmon() seems easy enough. If I were in a data frame this would yield a fine character variable, but in zoo tragedy ensues if you forget to wrap in as.numeric()

tz$yrmo <- as.numeric(as.yearmon(index(tstz)))
> head(tz)
           x y     yrmo
2008-01-02 7 2 2008.000
2008-01-03 7 3 2008.000
2008-02-04 1 4 2008.083
2008-02-07 6 7 2008.083
2008-03-08 0 8 2008.167
2008-03-09 9 9 2008.167

This looks great and I can compare data elements successfully

(tz$x[6] != tz$y[6])
2008-03-09
FALSE

but why do I get this result when I compare the year/month variable?

> (tz$yrmo[2] != tz$yrmo[1])
Data:
logical(0)

Index:
character(0)

and why does testing the yearmon or data items with identical() fail in this way? (both should be true)

> identical(tz$yrmo[2] , tz$yrmo[1])
[1] FALSE
> identical(tz$x[2] , tz$x[1])
[1] FALSE

Am I just playing with fire in using yearmon() which creates an index class in zoo?
Should I switch to something like Dirk Eddelbuettel's 'turning a date into a monthnumber'? Number of months between two dates

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2024-12-10 04:45:56

Q1:输出中包含数据和索引部分的线索是这些是动物园对象。所以它们也有比较的索引属性,但它们不相等。如果您想比较这些值,那么您可以访问 coredata():

> (coredata(tz$yrmo[2]) != coredata(tz$yrmo[1]))
[1] FALSE
> coredata(tz$yrmo[2])
[1] 2008
> coredata(tz$yrmo[1])
[1] 2008

Q2:identical 检查的不仅仅是数值。它还确定所有属性的平等。

> attributes(tz$yrmo[2])
$index
[1] "2008-01-03"

$class
[1] "zoo"

Q1: The clue in the output having a Data and a Index section is that these are zoo objects. So they have Index attributes that are compared as well and they are not equal. If you wanted to compare the values then you could access the coredata():

> (coredata(tz$yrmo[2]) != coredata(tz$yrmo[1]))
[1] FALSE
> coredata(tz$yrmo[2])
[1] 2008
> coredata(tz$yrmo[1])
[1] 2008

Q2: identical checks more than just the numeric values. It also determines equality of all attributes.

> attributes(tz$yrmo[2])
$index
[1] "2008-01-03"

$class
[1] "zoo"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文