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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:37:20+00:00 2026-05-26T15:37:20+00:00

Given a vector of datetime values, I needed to create a data.frame containing datetimes

  • 0

Given a vector of datetime values, I needed to create a data.frame containing datetimes at 0:10 hours past each original datetime – the first column 0 hours past, the second column 1 hour past, etc.

I had some trouble finding a way to do this easily using lubridate stuff. I thought this should work:

rt <- ymd_hms(c("2011-11-03 19:24:12", "2011-10-28 20:48:21",
  "2011-11-04 10:06:14", "2011-10-31 17:10:05", "2011-10-28 06:35:59"))
result <- outer(rt, hours(0:10), "+")

But various parts in that pipeline break down. Ultimately I get this error:

Error in FUN(X[[1L]], ...) : invalid 'times' argument

which seems to come from rep.POSIXct()‘s or rep.period()‘s inability to handle a non-unit-length times argument. Or something.

And it probably wouldn’t have worked anyway, because outer() returns a matrix, and date objects, even POSIXct dates (which are internally just integers) it seems can’t be elements in a matrix.

What I figured out that worked (just to get the times, not to put them in a data frame), after about 10 other guesses, was this:

with_tz(do.call(c, lapply(rt, function(x) x+hours(0:3))), tz(rt[1]))

The with_tz() addition is necessary because c() loses the timezone attribute. I also have to do do.call(c, lapply(...)) rather than just sapply(...) because sapply() loses the fact that it’s a date.

Maybe another alternative would be to create a data frame by doing do.call(cbind, ...) or something.

In general, it would be great if, whenever we find R date/time tasks that seem conceptually easy but require a lot of gymnastics before finding a solution, we could remove the roadblocks by making changes to lubridate, or whatever. I’m thinking this might be one of those times. =)

  • 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-26T15:37:21+00:00Added an answer on May 26, 2026 at 3:37 pm

    This doesn’t use outer(), but I think it gets you where you want. It does use plyr.

    library("lubridate")
    library("plyr")
    
    rt <- ymd_hms(c("2011-11-03 19:24:12", "2011-10-28 20:48:21",
      "2011-11-04 10:06:14", "2011-10-31 17:10:05", "2011-10-28 06:35:59"))
    
    offsets = 0:10
    names(offsets) <- offsets
    
    dat <- data.frame(llply(offsets, function(offset){rt+hours(offset)}))
    

    Giving names to the offsets variable just makes the column names of the data.frame nicer.

    > str(dat)
    'data.frame':   5 obs. of  11 variables:
     $ X0 : POSIXct, format: "2011-11-03 19:24:12" "2011-10-28 20:48:21" ...
     $ X1 : POSIXct, format: "2011-11-03 20:24:12" "2011-10-28 21:48:21" ...
     $ X2 : POSIXct, format: "2011-11-03 21:24:12" "2011-10-28 22:48:21" ...
     $ X3 : POSIXct, format: "2011-11-03 22:24:12" "2011-10-28 23:48:21" ...
     $ X4 : POSIXct, format: "2011-11-03 23:24:12" "2011-10-29 00:48:21" ...
     $ X5 : POSIXct, format: "2011-11-04 00:24:12" "2011-10-29 01:48:21" ...
     $ X6 : POSIXct, format: "2011-11-04 01:24:12" "2011-10-29 02:48:21" ...
     $ X7 : POSIXct, format: "2011-11-04 02:24:12" "2011-10-29 03:48:21" ...
     $ X8 : POSIXct, format: "2011-11-04 03:24:12" "2011-10-29 04:48:21" ...
     $ X9 : POSIXct, format: "2011-11-04 04:24:12" "2011-10-29 05:48:21" ...
     $ X10: POSIXct, format: "2011-11-04 05:24:12" "2011-10-29 06:48:21" ...
    

    UPDATE:

    Ken’s comment about ldply() versus data.frame(llply()) made me realize there is another way to approach this.

    dat <- ldply(rt, `+`, hours(0:10))
    

    which gives

    > str(dat)
    'data.frame':   5 obs. of  11 variables:
     $ V1 : POSIXct, format: "2011-11-03 12:24:12" "2011-10-28 13:48:21" ...
     $ V2 : POSIXct, format: "2011-11-03 13:24:12" "2011-10-28 14:48:21" ...
     $ V3 : POSIXct, format: "2011-11-03 14:24:12" "2011-10-28 15:48:21" ...
     $ V4 : POSIXct, format: "2011-11-03 15:24:12" "2011-10-28 16:48:21" ...
     $ V5 : POSIXct, format: "2011-11-03 16:24:12" "2011-10-28 17:48:21" ...
     $ V6 : POSIXct, format: "2011-11-03 17:24:12" "2011-10-28 18:48:21" ...
     $ V7 : POSIXct, format: "2011-11-03 18:24:12" "2011-10-28 19:48:21" ...
     $ V8 : POSIXct, format: "2011-11-03 19:24:12" "2011-10-28 20:48:21" ...
     $ V9 : POSIXct, format: "2011-11-03 20:24:12" "2011-10-28 21:48:21" ...
     $ V10: POSIXct, format: "2011-11-03 21:24:12" "2011-10-28 22:48:21" ...
     $ V11: POSIXct, format: "2011-11-03 22:24:12" "2011-10-28 23:48:21" ...
    

    Note that, in addition to different column names (V1-V11 rather than X0-X10), these dates have been converted to local time (PDT, in my case):

    > dat$V1
    [1] "2011-11-03 12:24:12 PDT" "2011-10-28 13:48:21 PDT"
    [3] "2011-11-04 03:06:14 PDT" "2011-10-31 10:10:05 PDT"
    [5] "2011-10-27 23:35:59 PDT"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that processes a given vector, but may also create such
Given a sorted vector with a number of values, as in the following example:
Given a multimap<A,B> M what's a neat way to create a vector<B> of all
This question is about the Data.Vector package. Given the fact that I'll never use
Given a vector of variables, transform the vector so that each variable is repeated
Given a vector of variables, transform the vector so that each variable is repeated
Given a vector space V, a basis B (list of tuples each of size
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
In R, given a vector casp6 <- c(0.9478638, 0.7477657, 0.9742675, 0.9008372, 0.4873001, 0.5097587, 0.6476510,
Given an STL vector, output only the duplicates in sorted order, e.g., INPUT :

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.