循环遍历R中的日期向量可丢弃类信息 [英] Looping through vector of dates in R drops class info
本文介绍了循环遍历R中的日期向量可丢弃类信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的例子.
my_df <- data.frame(col_1 = c(1,2),
col_2 = c(as.Date('2018-11-11'), as.Date('2016-01-01')))
dates_list <- my_df$col_2
for(el in dates_list){
print(el)
}
它生成:
17846
16801
如何改为输出日期?我可以使用显式索引来做到这一点,但希望有一个更简单的解决方案
How do I output dates instead? I can do it with explicit index, but would like to have simpler solution
推荐答案
1)使用as.list
:
for(el in as.list(dates_list)) {
print(el)
}
给予:
[1] "2018-11-11"
[1] "2016-01-01"
2)还是不太好,但是可以遍历索引:
2) or not quite as nice but one can iterate over the indexes:
for(i in seq_along(dates_list)) {
print(dates_list[i])
}
这篇关于循环遍历R中的日期向量可丢弃类信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文