我正在以在线方式处理时间序列,所以我需要按顺序提取日期(一次一个)。我用过:
dates<- seq(as.Date(today), by="days", length=10)
dates[1]
[,1]
2016-06-24 NA
我想像在矩阵中那样提取每个元素,例如:
mat<- matrix(c(1,2),ncol=1, nrow=2)
mat[1,1]
[1] 1
如何通过索引或循环获取日期部分,即2016-06-24?
答案 0 :(得分:1)
如果我误解了你的问题,请告诉我。这会将它们按顺序排列并将其打印到屏幕上:
dates<- seq(as.Date(Sys.Date()), by="days", length=10)
for(i in 1:length(dates)){
print(paste("I have extracted the following date:", dates[i]))
}
[1] "I have extracted the following date: 2016-06-25"
[1] "I have extracted the following date: 2016-06-26"
[1] "I have extracted the following date: 2016-06-27"
[1] "I have extracted the following date: 2016-06-28"
[1] "I have extracted the following date: 2016-06-29"
[1] "I have extracted the following date: 2016-06-30"
[1] "I have extracted the following date: 2016-07-01"
[1] "I have extracted the following date: 2016-07-02"
[1] "I have extracted the following date: 2016-07-03"
[1] "I have extracted the following date: 2016-07-04"
答案 1 :(得分:0)
使用paste()
函数一次打印所有文本并将其与dates
对象合并。
today <- Sys.Date()
today
# [1] "2016-06-26"
dates<- seq(as.Date(today), by="days", length=10)
dates
# [1] "2016-06-26" "2016-06-27" "2016-06-28" "2016-06-29" "2016-06-30"
# [6] "2016-07-01" "2016-07-02" "2016-07-03" "2016-07-04" "2016-07-05"
paste("I have extracted the following date: ",dates)
# [1] "I have extracted the following date: 2016-06-26"
# [2] "I have extracted the following date: 2016-06-27"
# [3] "I have extracted the following date: 2016-06-28"
# [4] "I have extracted the following date: 2016-06-29"
# [5] "I have extracted the following date: 2016-06-30"
# [6] "I have extracted the following date: 2016-07-01"
# [7] "I have extracted the following date: 2016-07-02"
# [8] "I have extracted the following date: 2016-07-03"
# [9] "I have extracted the following date: 2016-07-04"
#[10] "I have extracted the following date: 2016-07-05"