[英]looping through list of dates in R
Here is my example: 这是我的示例:
test_list <- c(as.Date('2017-03-08','2017-03-08' ))
for(el in test_list){
print(el)
}
It generates: 17641 它生成:17641
This is date representation. 这是日期表示。 I am curious why R behaves this way. 我很好奇R为什么如此行事。
What exactly do you want to get back? 您到底想得到什么? One time '2017-03-08' or two times as in the code below? 一次“ 2017-03-08”还是两次,如下面的代码所示?
test_list <- as.Date(c('2017-03-08','2017-03-08'))
for(el in test_list){
print(as.Date(el, origin = "1970-01-01"))
}
Pierre Lapointe is correct that calling multiple arguments with in as.Date
is part of your issue. Pierre Lapointe是正确的,使用as.Date
调用多个参数是您问题的一部分。
The number 17641
has to do with how R stores dates . 数字17641
与R如何存储日期有关 。 Calling test_list
yields "2018-04-20"
, which I would guess means it's added those two dates. 调用test_list
产生"2018-04-20"
test_list
"2018-04-20"
,我想这意味着它已经添加了这两个日期。
The origin date in R is 1970-01-01
. R中的起源日期是1970-01-01
。 If you add 17641
to that, you get 2018-04-20
. 如果您添加17641
,则得到2018-04-20
。 So R is returning the integer corresponding to your date, which can be verified by calling class(el)
, which makes sense because in a loop your are referring to elements in test_list
by their ordinality (ordinance?) 因此,R返回的是与您的日期相对应的整数,可以通过调用class(el)
进行验证,这很有意义,因为在循环中,您通过test_list
引用了test_list
的元素( test_list
?)。
The following worked fine for me. 以下对我来说很好。 I'm not exactly sure why, but if it ain't broke, don't fix it! 我不确定为什么,但是如果它没有损坏,请不要修复它!
for (el in 1:length(test_list)){
print(test_list[el])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.