为什么'for'循环会改变序列值的类型?

时间:2014-04-24 19:26:20

标签: r date for-loop attributes

为什么' for'循环似乎转换它从"日期"序列中提取的项目。第一个示例中的项目为数字,但如果我创建了一个" list"不按顺序?

> dates <- seq(as.Date('2014-3-9'), as.Date('2014-3-12'), by = '1 day')
> str(dates)
 Date[1:4], format: "2014-03-09" "2014-03-10" "2014-03-11" "2014-03-12"
> # seems to convert "Date" to "numeric" when extracting the item in the loop
> for (i in dates){ 
+     str(i)
+     print(i)
+ }
 num 16138
[1] 16138
 num 16139
[1] 16139
 num 16140
[1] 16140
 num 16141
[1] 16141
> 
> # works fine if I put it in a 'list'
> for (i in as.list(dates)){ 
+     str(i)
+     print(i)
+ }
 Date[1:1], format: "2014-03-09"
[1] "2014-03-09"
 Date[1:1], format: "2014-03-10"
[1] "2014-03-10"
 Date[1:1], format: "2014-03-11"
[1] "2014-03-11"
 Date[1:1], format: "2014-03-12"
[1] "2014-03-12"
> 
> 

0 个答案:

没有答案