Can someone help clarify what was being output when this for loop was originally specified (incorrectly)?
dates <- seq.Date(as.Date("2023-01-01"), as.Date("2023-01-07"), by = "day")
Original:
for(mydate in (dates)) {
print(mydate)
}
Original output (what are these??):
[1] 19358
[1] 19359
[1] 19360
[1] 19361
[1] 19362
[1] 19363
[1] 19364
Correct:
for(mydate in 1:length(mydates)) {
print(mydate)
}
Desired output:
[1] 1
[1] 2
[1] 3 .... etc
What is the 19358, 19359 etc? Some kind of weird index?
for loops use low level subsetting, ditching the class, like .subset2()
would.
dates <- seq.Date(as.Date("2023-01-01"), as.Date("2023-01-07"), by = "day")
# what your object is made of at the lower level
dput(dates)
#> structure(c(19358, 19359, 19360, 19361, 19362, 19363, 19364), class = "Date")
structure(c(19358, 19359, 19360, 19361, 19362, 19363, 19364), class = "Date")
#> [1] "2023-01-01" "2023-01-02" "2023-01-03" "2023-01-04" "2023-01-05"
#> [6] "2023-01-06" "2023-01-07"
# bracket subsetting
dates[[1]]
#> [1] "2023-01-01"
# low level subsetting, fetch element, ditch attributes of containing list/vector, attributes include class
.subset2(dates, 1)
#> [1] 19358