0

Can someone explain why am I getting different date class in the for loop below?

dt_all <- Sys.Date() + 1:2

for(dt in dt_all){
  print(dt)
  cat("This is of",class(dt),"class\n")
}
# [1] 17723
# This is of numeric class
# [1] 17724
# This is of numeric class

for(i in 1:length(dt_all)){
  dt <- dt_all[i]
  print(dt)
  cat("This is of",class(dt),"class\n")
}

# [1] "2018-07-11"
# This is of Date class
# [1] "2018-07-12"
# This is of Date class

Thanks in advance.

Raja
  • 157
  • 1
  • 11

1 Answers1

0

probably because you can't loop through, date class objects, so r converts them to a numeric.

skatz
  • 115
  • 7
  • I have seen similar situation for factor variable. Try this..dt_all <- factor(x = Sys.Date() + 1:3, labels=LETTERS[1:3]) – Raja Jul 10 '18 at 07:28