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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:46:21+00:00 2026-05-31T20:46:21+00:00

I’m trying to set a time value into a data frame: ps = data.frame(t(rep(NA,

  • 0

I’m trying to set a time value into a data frame:

ps = data.frame(t(rep(NA, 2)))
ps[1,1] = strptime('10:30:00', '%H:%M:%S')

but I get the error:

provided 9 variables to replace 1 variables

since a time value is a list (?) in R it thinks I’m trying to set 9 columns, when I really just want to set the one column to that class.

What can I do to make this set properly?

  • 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-31T20:46:22+00:00Added an answer on May 31, 2026 at 8:46 pm

    This is due to the result of strptime() being an object of class "POSIXlt":

    > ps = data.frame(t(rep(NA, 2)))
    > ps[1,1] = strptime('10:30:00', '%H:%M:%S')
    Warning message:
    In `[<-.data.frame`(`*tmp*`, 1, 1, value = list(sec = 0, min = 30L,  :
      provided 9 variables to replace 1 variables
    > strptime('10:30:00', '%H:%M:%S')
    [1] "2012-03-21 10:30:00"
    > class(strptime('10:30:00', '%H:%M:%S'))
    [1] "POSIXlt" "POSIXt"
    

    A "POSIXlt" object is a list representation (hence the lt rather than the ct in the class name) of the time:

    > foo <- strptime('10:30:00', '%H:%M:%S')
    > str(foo)
     POSIXlt[1:1], format: "2012-03-21 10:30:00"
    > unclass(foo)
    $sec
    [1] 0
    
    $min
    [1] 30
    
    $hour
    [1] 10
    
    $mday
    [1] 21
    
    $mon
    [1] 2
    
    $year
    [1] 112
    
    $wday
    [1] 3
    
    $yday
    [1] 80
    
    $isdst
    [1] 0
    

    A "POSIXlt" object is a list of length 9:

    > length(unclass(foo))
    [1] 9
    

    hence the warning message, as the object is being stripped back to it’s constituent parts/representation. You can stick the "POSIXct" representation in instead without generating the warning:

    > ps[1,1] = as.POSIXct(strptime('10:30:00', '%H:%M:%S'))
    > ps[1,1]
    [1] 1332325800
    

    but we are still loosing the class information. Still, you can go back to the "POSIXct" representation later using the as.POSIXct() function but you will need to specify the origin argument. See ?POSIXct for more.

    > class(ps[1,1])
    [1] "numeric"
    

    A solution is to coerce ps$X1 to be class "POSIXct" before inserting the time:

    > ps = data.frame(t(rep(NA, 2)))
    > ps <- transform(ps, X1 = as.POSIXct(X1))
    > ps[1,1] <- as.POSIXct(strptime('10:30:00', '%H:%M:%S'))
    > ps
                       X1 X2
    1 2012-03-21 10:30:00 NA
    > str(ps)
    'data.frame':   1 obs. of  2 variables:
     $ X1: POSIXct, format: "2012-03-21 10:30:00"
     $ X2: logi NA
    

    No warning (as before with as.POSIXct()) but also the class information is retained, where before it was lost. Do read ?`[.data.frame`, especially the Coercion section which has some details; but my take how is that understanding the coercion in replacements like this is tricky.

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

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.