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

  • Home
  • SEARCH
  • 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 9239811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:04:30+00:00 2026-06-18T08:04:30+00:00

I have a very basic question regarding vectors in R. I want to create

  • 0

I have a very basic question regarding vectors in R. I want to create an empty vector and append dates to it. However, R converts the dates to “numeric”, i.e.

library(lubridate)
a <- c()
a <- c(a,now())
class(a)

results in

[1] "numeric"

Meanwhile, this code does exactly what I want:

  a <- c(now())
  a <- c(a,now())
  class(a)

i.e. the class is correct now:

[1] “POSIXct” “POSIXt”

The problem is that I don’t want to initialize my vector with any date, i.e. I want it to be empty in the beginning.

I´ve tried to use list and then to “unlist” it (because I want to I want to pass these dates as an argument to functions like max() after) but it also gives me numerics:

a  <- list()
a[[1]] <- now()
a[[2]] <- now()
class(unlist(a))

Using array doesn’t help me either.

Thus I’m a bit stuck. I’ve read the documentation regarding output type of vector in r but couldn’t find any solution. How can I create an empty vecto of dates, append a few dates and get the dates in the end? 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-06-18T08:04:32+00:00Added an answer on June 18, 2026 at 8:04 am

    Remember that when you use c() you don’t initialize a vector, but you create a NULL value. You need to use vector() to initiate a vector where you can manipulate the classes:

    > identical(c(),NULL)
    [1] TRUE
    > identical(c(),vector())
    [1] FALSE
    

    Next to that, the solution of @juba is fine as long as there’s no timezone involved. If there is, you get the following :

    > a <- vector()
    > class(a) <- 'POSIXct'
    > b <- as.POSIXct(as.character(Sys.time()),tz="GMT")
    > b
    [1] "2013-01-29 14:10:32 GMT"
    > c(a,b)
    [1] "2013-01-29 15:10:32 CET"
    

    In order to avoid this, you better copy the attributes, like this :

    > attributes(X) <- attributes(b)
    > a <- vector()
    > b <- as.POSIXct(as.character(Sys.time()),tz="GMT")
    > X <- c(a,b)
    > attributes(X) <- attributes(b)
    > X
    [1] "2013-01-29 14:10:32 GMT"
    

    But in any case you shouldn’t be considering this at all, for the simple reason that appending a vector is a very slow process that can get you into trouble. If you have to save 100 dates in a vector, you better use either an lapply/sapply solution as Paul Hiemstra suggested, or you initiate your vector like :

    > a <- vector("numeric",100)
    > class(a) <- c('POSIXct','POSIXt')
    

    or

    > a <- vector("numeric",100)
    > attributes(a) <- list(class=c("POSIXct","POSIXt"),tzone="CET")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic question regarding timers. My timer is acting very strange. I
I have very basic question regarding return value of a function, and checking the
I have a very basic question regarding properties in Objective-C. I can only access
I have a very simply question regarding IEquatable. Given the following basic classes: public
I have a very basic question regarding to arrays in numpy, but I cannot
I have a very basic question regarding the design of a database. I thought
I'm a perl noob and I have a very basic question regarding the @INC
I have a very, i think basic, question regarding CSS with floating div objects.
I have a very basic question regarding plotting pseudocolor plots in matlab. Say if
I have a very basic question. Lets take this snippet: #include <stdio.h> void foo(void)

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.