Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:43:27+00:00 2026-06-17T20:43:27+00:00

This is related to Looping over a Date or POSIXct object results in a

  • 0

This is related to Looping over a Date or POSIXct object results in a numeric iterator

> dates <- as.Date(c("2013-01-01", "2013-01-02"))
> class(dates)
[1] "Date"
> for(d in dates) print(class(d))
[1] "numeric"
[1] "numeric"

I have two questions:

  1. What is the preferred way to iterate over a list of Date objects?
  2. I don’t understand Joshua’s answer (accepted answer from the question linked above), I’ll quote it here: "So your Date vector is being coerced to numeric because Date objects aren’t strictly vectors". So how is it determined that Date should be coerced to numeric?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:43:28+00:00Added an answer on June 17, 2026 at 8:43 pm

    There are two issues here. One is whether the input gets coerced from Date to numeric. The other is whether the output gets coerced to numeric.

    Input

    For loops coerce Date inputs to numeric, because as @DWin and @JoshuaUlrich point out, for loops take vectors, and Dates are technically not vectors.

    > for(d in dates) print(class(d))
    [1] "numeric"
    [1] "numeric"
    

    On the other hand, lapply and its simplifier offspring sapply have no such restrictions.

    > sapply( dates, function(day) class(day) )
    [1] "Date" "Date"
    

    Output

    However! The output of class() above is a character. If you try actually returning a date object, sapply is not what you want.

    lapply does not coerce to a vector, but sapply does:

    > lapply( dates, identity )
    [[1]]
    [1] "2013-01-01"
    
    [[2]]
    [1] "2013-01-02"
    
    > sapply( dates, identity )
    [1] 15706 15707
    

    That’s because sapply‘s simplification function coerces output to a vector.

    Summary

    So: If you have a Date object and want to return a non-Date object, you can use lapply or sapply. If you have a non-Date object, and want to return a Date object, you can use a for loop or lapply. If you have a Date object and want to return a Date object, use lapply.

    Resources for learning more

    If you want to dig deeper into vectors, you can start with John Cook’s notes, continue with the R Inferno, and continue with SDA.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to another Delphi-version question but still different; I'm looking for a
This question is an extension of this Related question . Taking Derick's advice, I
I found this related question: How do I use composition with inheritance? I would
I have searched here, GooBingHooVista'd the world and read this related question for VS
This is related to this question but with a different take. In Ubuntu, I
This is related to OpenId authentication. I have implemented the google and yahoo openId
This is related to Clang 3.1 and C++11 support status , but I could
This is related to my other question but I am openning a new one
This is related to this question I asked earlier about syntax highlighting user-defined blocks
This is related to my previous question I'm solving UVA's Edit Step Ladders and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.