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 7087915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:43:58+00:00 2026-05-28T07:43:58+00:00

> my.lt <- strptime(2003-02-05 03:00:02, format=%Y-%m-%d %H:%M:%S) > x <- data.frame(d=my.lt) > class(x$d) [1]

  • 0
> my.lt <- strptime("2003-02-05 03:00:02", format="%Y-%m-%d %H:%M:%S")
> x <- data.frame(d=my.lt)
> class(x$d)
[1] "POSIXct" "POSIXt" 

I don’t know why data.frame changed x$d from a POSIXlt object to a POSIXct one. Now if I do

> x$d = my.lt

Then I got what I want, but this is ugly. Can anybody tell me 1) Why this happened; and 2) How to initialize a data frame with one of its column being a POSIXlt in a neat way.

Thank you.

  • 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-05-28T07:43:59+00:00Added an answer on May 28, 2026 at 7:43 am

    As it says in the 3rd paragraph of the Details section of ?data.frame:

    ‘data.frame’ converts each of its arguments to a data frame by calling ‘as.data.frame(optional=TRUE)’.

    That means as.data.frame.POSIXlt is being called. It’s defined as:

    function (x, row.names = NULL, optional = FALSE, ...) 
    {
        value <- as.data.frame.POSIXct(as.POSIXct(x), row.names, 
            optional, ...)
        if (!optional) 
            names(value) <- deparse(substitute(x))[[1L]]
        value
    }
    

    So that’s why it happened. I can’t think of a clean way to do it using the data.frame constructor, but here is a bit of a kludge:

    x <- data.frame(d=as.data.frame.vector(my.lt))
    

    This converts your POSIXlt object to a data.frame using the vector method. If you really want to confuse yourself later, you can also use the POSIXct method:

    x <- data.frame(d=as.data.frame.POSIXct(my.lt))
    str(x)
    # 'data.frame':   1 obs. of  1 variable:
    #  $ my.lt: POSIXlt, format: "2003-02-05 03:00:02"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a function to convert datetimes from one format to another - int
From this answer , I'm using DateTime::Format::Strptime to grab the date from a string.
I get time data '19/Apr/2011:22:12:39' does not match format '%d/%b/%y:%H:%M:%S' when using datetime.strptime('19/Apr/2011:22:12:39','%d/%b/%y:%H:%M:%S') What
I also have a week number, extracted from another time object like this other_time.strftime('%W').to_i
> time1 = strptime(2010-03-01 00:15:00, format = %Y-%m-%d %H:%M:%S) > time2a = strptime(2010-03-01, format
I am trying to convert a date in a particular format using strptime ,
options(digits.secs = 3); > strptime(2007-03-30 15:00:00.007, format = %Y-%m-%d %H:%M:%OS); [1] 2007-03-30 15:00:00.007 >
In time.strftime(format[, t]) the first parameter is format and in time.strptime(string[, format]) it is
I'm getting a result I don't understand in R. If I use strptime with
My string format currently is datetime.strptime(date_as_string, '%d/%m/%y %I:%M %p') this unfortunately does not work

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.