Alexey Kalmykov
Alexey Kalmykov

Reputation: 1958

Output type of vector in R

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.

Upvotes: 4

Views: 368

Answers (3)

Joris Meys
Joris Meys

Reputation: 108583

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")

Upvotes: 6

Paul Hiemstra
Paul Hiemstra

Reputation: 60964

Generally, there is no need to start with an empty vector. You can create a vector with dates like this:

date_vec = lapply(1:10, function(x) return(Sys.time()))
> date_vec                                                                  
[[1]]                                                                       
[1] "2013-01-29 13:53:29 CET"                                               

[[2]]                                                                       
[1] "2013-01-29 13:53:29 CET"                                               

[[3]]                                                                       
[1] "2013-01-29 13:53:29 CET"                                               

[[4]]                                                                       
[1] "2013-01-29 13:53:29 CET"                                               

[[5]]
[1] "2013-01-29 13:53:29 CET"

[[6]]
[1] "2013-01-29 13:53:29 CET"

[[7]]
[1] "2013-01-29 13:53:29 CET"

[[8]]
[1] "2013-01-29 13:53:29 CET"

[[9]]
[1] "2013-01-29 13:53:29 CET"

[[10]]
[1] "2013-01-29 13:53:29 CET"

Or, I often create the vectors of dates from strings representing dates using strptime.

Upvotes: 1

juba
juba

Reputation: 49033

You can try this :

library(lubridate)
a <- vector()
class(a) <- "POSIXct"
a <- c(a, now())
class(a)
[1] "POSIXct" "POSIXt" 

Upvotes: 2

Related Questions