How to make an empty vector of POSIXct

后端 未结 6 1308
孤街浪徒
孤街浪徒 2021-02-12 03:21

I want to make an empty vector of POSIXct so that I can put a POSIXct in it:

vec <- vector(\"POSIXct\", 10)
vec
vec[1] <- \"2014-         


        
6条回答
  •  花落未央
    2021-02-12 04:16

    Since there is no POSIX mode, you cannot initialize a POSIXct vector with vector() alone (see ?mode for a list of all mode types).

    But we can use .POSIXct to create the vector from a character vector.

    (x <- .POSIXct(character(10))) ## the same as .POSIXct(vector("character", 10))
    # [1] NA NA NA NA NA NA NA NA NA NA
    class(x)
    # [1] "POSIXct" "POSIXt" 
    

    Also note that you can also use .POSIXct(integer(10)) for a length 10 vector of origin date-times.

提交回复
热议问题